Blame view

mm/vmscan.c 126 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   *  linux/mm/vmscan.c
   *
   *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
   *
   *  Swap reorganised 29.12.95, Stephen Tweedie.
   *  kswapd added: 7.1.96  sct
   *  Removed kswapd_ctl limits, and swap out as many pages as needed
   *  to bring the system back to freepages.high: 2.4.97, Rik van Riel.
   *  Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com).
   *  Multiqueue VM started 5.8.00, Rik van Riel.
   */
b1de0d139   Mitchel Humpherys   mm: convert some ...
14
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
  #include <linux/mm.h>
5b3cc15af   Ingo Molnar   sched/headers: Pr...
16
  #include <linux/sched/mm.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
  #include <linux/module.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
18
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
23
  #include <linux/kernel_stat.h>
  #include <linux/swap.h>
  #include <linux/pagemap.h>
  #include <linux/init.h>
  #include <linux/highmem.h>
70ddf637e   Anton Vorontsov   memcg: add memory...
24
  #include <linux/vmpressure.h>
e129b5c23   Andrew Morton   [PATCH] vm: add p...
25
  #include <linux/vmstat.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
30
31
  #include <linux/file.h>
  #include <linux/writeback.h>
  #include <linux/blkdev.h>
  #include <linux/buffer_head.h>	/* for try_to_release_page(),
  					buffer_heads_over_limit */
  #include <linux/mm_inline.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
36
  #include <linux/backing-dev.h>
  #include <linux/rmap.h>
  #include <linux/topology.h>
  #include <linux/cpu.h>
  #include <linux/cpuset.h>
3e7d34497   Mel Gorman   mm: vmscan: recla...
37
  #include <linux/compaction.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
39
  #include <linux/notifier.h>
  #include <linux/rwsem.h>
248a0301e   Rafael J. Wysocki   [PATCH] mm: make ...
40
  #include <linux/delay.h>
3218ae14b   Yasunori Goto   [PATCH] pgdat all...
41
  #include <linux/kthread.h>
7dfb71030   Nigel Cunningham   [PATCH] Add inclu...
42
  #include <linux/freezer.h>
66e1707bc   Balbir Singh   Memory controller...
43
  #include <linux/memcontrol.h>
873b47717   Keika Kobayashi   per-task-delay-ac...
44
  #include <linux/delayacct.h>
af936a160   Lee Schermerhorn   vmscan: unevictab...
45
  #include <linux/sysctl.h>
929bea7c7   KOSAKI Motohiro   vmscan: all_unrec...
46
  #include <linux/oom.h>
64e3d12f7   Kuo-Hsin Yang   mm, drm/i915: mar...
47
  #include <linux/pagevec.h>
268bb0ce3   Linus Torvalds   sanitize <linux/p...
48
  #include <linux/prefetch.h>
b1de0d139   Mitchel Humpherys   mm: convert some ...
49
  #include <linux/printk.h>
f9fe48bec   Ross Zwisler   dax: support dirt...
50
  #include <linux/dax.h>
eb414681d   Johannes Weiner   psi: pressure sta...
51
  #include <linux/psi.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
55
56
  
  #include <asm/tlbflush.h>
  #include <asm/div64.h>
  
  #include <linux/swapops.h>
117aad1e9   Rafael Aquini   mm: avoid reinser...
57
  #include <linux/balloon_compaction.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58

0f8053a50   Nick Piggin   [PATCH] mm: make ...
59
  #include "internal.h"
33906bc5c   Mel Gorman   vmscan: tracing: ...
60
61
  #define CREATE_TRACE_POINTS
  #include <trace/events/vmscan.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
  struct scan_control {
22fba3354   KOSAKI Motohiro   vmscan: separate ...
63
64
  	/* How many pages shrink_list() should reclaim */
  	unsigned long nr_to_reclaim;
ee814fe23   Johannes Weiner   mm: vmscan: clean...
65
66
67
68
69
  	/*
  	 * Nodemask of nodes allowed by the caller. If NULL, all nodes
  	 * are scanned.
  	 */
  	nodemask_t	*nodemask;
9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
70

5f53e7629   KOSAKI Motohiro   vmscan: page_chec...
71
  	/*
f16015fbf   Johannes Weiner   mm: vmscan: disti...
72
73
74
75
  	 * The memory cgroup that hit its limit and as a result is the
  	 * primary target of this reclaim invocation.
  	 */
  	struct mem_cgroup *target_mem_cgroup;
66e1707bc   Balbir Singh   Memory controller...
76

1276ad68e   Johannes Weiner   mm: vmscan: scan ...
77
  	/* Writepage batching in laptop mode; RECLAIM_WRITE */
ee814fe23   Johannes Weiner   mm: vmscan: clean...
78
79
80
81
82
83
84
  	unsigned int may_writepage:1;
  
  	/* Can mapped pages be reclaimed? */
  	unsigned int may_unmap:1;
  
  	/* Can pages be swapped as part of reclaim? */
  	unsigned int may_swap:1;
d6622f636   Yisheng Xie   mm/vmscan: more r...
85
86
87
88
89
90
91
  	/*
  	 * Cgroups are not reclaimed below their configured memory.low,
  	 * unless we threaten to OOM. If any cgroups are skipped due to
  	 * memory.low and nothing was reclaimed, go back for memory.low.
  	 */
  	unsigned int memcg_low_reclaim:1;
  	unsigned int memcg_low_skipped:1;
241994ed8   Johannes Weiner   mm: memcontrol: d...
92

ee814fe23   Johannes Weiner   mm: vmscan: clean...
93
94
95
96
  	unsigned int hibernation_mode:1;
  
  	/* One of the zones is ready for compaction */
  	unsigned int compaction_ready:1;
bb451fdf3   Greg Thelen   mm/vmscan.c: cond...
97
98
99
100
101
102
103
104
105
106
107
  	/* Allocation order */
  	s8 order;
  
  	/* Scan (total_size >> priority) pages at once */
  	s8 priority;
  
  	/* The highest zone to isolate pages for reclaim from */
  	s8 reclaim_idx;
  
  	/* This context's GFP mask */
  	gfp_t gfp_mask;
ee814fe23   Johannes Weiner   mm: vmscan: clean...
108
109
110
111
112
  	/* Incremented by the number of inactive pages that were scanned */
  	unsigned long nr_scanned;
  
  	/* Number of pages freed so far during a call to shrink_zones() */
  	unsigned long nr_reclaimed;
d108c7721   Andrey Ryabinin   mm/vmscan: don't ...
113
114
115
116
117
118
119
120
121
122
  
  	struct {
  		unsigned int dirty;
  		unsigned int unqueued_dirty;
  		unsigned int congested;
  		unsigned int writeback;
  		unsigned int immediate;
  		unsigned int file_taken;
  		unsigned int taken;
  	} nr;
e5ca8071f   Yafang Shao   mm/vmscan.c: add ...
123
124
125
  
  	/* for recording the reclaimed slab by now */
  	struct reclaim_state reclaim_state;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  #ifdef ARCH_HAS_PREFETCH
  #define prefetch_prev_lru_page(_page, _base, _field)			\
  	do {								\
  		if ((_page)->lru.prev != _base) {			\
  			struct page *prev;				\
  									\
  			prev = lru_to_page(&(_page->lru));		\
  			prefetch(&prev->_field);			\
  		}							\
  	} while (0)
  #else
  #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0)
  #endif
  
  #ifdef ARCH_HAS_PREFETCHW
  #define prefetchw_prev_lru_page(_page, _base, _field)			\
  	do {								\
  		if ((_page)->lru.prev != _base) {			\
  			struct page *prev;				\
  									\
  			prev = lru_to_page(&(_page->lru));		\
  			prefetchw(&prev->_field);			\
  		}							\
  	} while (0)
  #else
  #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0)
  #endif
  
  /*
   * From 0 .. 100.  Higher means more swappy.
   */
  int vm_swappiness = 60;
d0480be44   Wang Sheng-Hui   mm: update the de...
159
160
161
162
163
  /*
   * The total number of pages which are beyond the high watermark within all
   * zones.
   */
  unsigned long vm_total_pages;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164

0a432dcbe   Yang Shi   mm: shrinker: mak...
165
166
167
168
169
170
171
172
173
174
175
  static void set_task_reclaim_state(struct task_struct *task,
  				   struct reclaim_state *rs)
  {
  	/* Check for an overwrite */
  	WARN_ON_ONCE(rs && task->reclaim_state);
  
  	/* Check for the nulling of an already-nulled member */
  	WARN_ON_ONCE(!rs && !task->reclaim_state);
  
  	task->reclaim_state = rs;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
177
  static LIST_HEAD(shrinker_list);
  static DECLARE_RWSEM(shrinker_rwsem);
0a432dcbe   Yang Shi   mm: shrinker: mak...
178
  #ifdef CONFIG_MEMCG
7e010df53   Kirill Tkhai   mm: use special v...
179
180
181
182
183
184
185
186
187
188
189
190
  /*
   * We allow subsystems to populate their shrinker-related
   * LRU lists before register_shrinker_prepared() is called
   * for the shrinker, since we don't want to impose
   * restrictions on their internal registration order.
   * In this case shrink_slab_memcg() may find corresponding
   * bit is set in the shrinkers map.
   *
   * This value is used by the function to detect registering
   * shrinkers and to skip do_shrink_slab() calls for them.
   */
  #define SHRINKER_REGISTERING ((struct shrinker *)~0UL)
b4c2b231c   Kirill Tkhai   mm: assign id to ...
191
192
193
194
195
196
197
198
199
  static DEFINE_IDR(shrinker_idr);
  static int shrinker_nr_max;
  
  static int prealloc_memcg_shrinker(struct shrinker *shrinker)
  {
  	int id, ret = -ENOMEM;
  
  	down_write(&shrinker_rwsem);
  	/* This may call shrinker, so it must use down_read_trylock() */
7e010df53   Kirill Tkhai   mm: use special v...
200
  	id = idr_alloc(&shrinker_idr, SHRINKER_REGISTERING, 0, 0, GFP_KERNEL);
b4c2b231c   Kirill Tkhai   mm: assign id to ...
201
202
  	if (id < 0)
  		goto unlock;
0a4465d34   Kirill Tkhai   mm, memcg: assign...
203
204
205
206
207
  	if (id >= shrinker_nr_max) {
  		if (memcg_expand_shrinker_maps(id)) {
  			idr_remove(&shrinker_idr, id);
  			goto unlock;
  		}
b4c2b231c   Kirill Tkhai   mm: assign id to ...
208
  		shrinker_nr_max = id + 1;
0a4465d34   Kirill Tkhai   mm, memcg: assign...
209
  	}
b4c2b231c   Kirill Tkhai   mm: assign id to ...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
  	shrinker->id = id;
  	ret = 0;
  unlock:
  	up_write(&shrinker_rwsem);
  	return ret;
  }
  
  static void unregister_memcg_shrinker(struct shrinker *shrinker)
  {
  	int id = shrinker->id;
  
  	BUG_ON(id < 0);
  
  	down_write(&shrinker_rwsem);
  	idr_remove(&shrinker_idr, id);
  	up_write(&shrinker_rwsem);
  }
b4c2b231c   Kirill Tkhai   mm: assign id to ...
227

89b5fae53   Johannes Weiner   mm: vmscan: disti...
228
229
  static bool global_reclaim(struct scan_control *sc)
  {
f16015fbf   Johannes Weiner   mm: vmscan: disti...
230
  	return !sc->target_mem_cgroup;
89b5fae53   Johannes Weiner   mm: vmscan: disti...
231
  }
97c9341f7   Tejun Heo   mm: vmscan: disab...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
  
  /**
   * sane_reclaim - is the usual dirty throttling mechanism operational?
   * @sc: scan_control in question
   *
   * The normal page dirty throttling mechanism in balance_dirty_pages() is
   * completely broken with the legacy memcg and direct stalling in
   * shrink_page_list() is used for throttling instead, which lacks all the
   * niceties such as fairness, adaptive pausing, bandwidth proportional
   * allocation and configurability.
   *
   * This function tests whether the vmscan currently in progress can assume
   * that the normal dirty throttling mechanism is operational.
   */
  static bool sane_reclaim(struct scan_control *sc)
  {
  	struct mem_cgroup *memcg = sc->target_mem_cgroup;
  
  	if (!memcg)
  		return true;
  #ifdef CONFIG_CGROUP_WRITEBACK
69234acee   Linus Torvalds   Merge branch 'for...
253
  	if (cgroup_subsys_on_dfl(memory_cgrp_subsys))
97c9341f7   Tejun Heo   mm: vmscan: disab...
254
255
256
257
  		return true;
  #endif
  	return false;
  }
e3c1ac586   Andrey Ryabinin   mm/vmscan: don't ...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  
  static void set_memcg_congestion(pg_data_t *pgdat,
  				struct mem_cgroup *memcg,
  				bool congested)
  {
  	struct mem_cgroup_per_node *mn;
  
  	if (!memcg)
  		return;
  
  	mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
  	WRITE_ONCE(mn->congested, congested);
  }
  
  static bool memcg_congested(pg_data_t *pgdat,
  			struct mem_cgroup *memcg)
  {
  	struct mem_cgroup_per_node *mn;
  
  	mn = mem_cgroup_nodeinfo(memcg, pgdat->node_id);
  	return READ_ONCE(mn->congested);
  
  }
91a45470f   KAMEZAWA Hiroyuki   per-zone and recl...
281
  #else
0a432dcbe   Yang Shi   mm: shrinker: mak...
282
283
284
285
286
287
288
289
  static int prealloc_memcg_shrinker(struct shrinker *shrinker)
  {
  	return 0;
  }
  
  static void unregister_memcg_shrinker(struct shrinker *shrinker)
  {
  }
89b5fae53   Johannes Weiner   mm: vmscan: disti...
290
291
292
293
  static bool global_reclaim(struct scan_control *sc)
  {
  	return true;
  }
97c9341f7   Tejun Heo   mm: vmscan: disab...
294
295
296
297
298
  
  static bool sane_reclaim(struct scan_control *sc)
  {
  	return true;
  }
e3c1ac586   Andrey Ryabinin   mm/vmscan: don't ...
299
300
301
302
303
304
305
306
307
308
309
310
  
  static inline void set_memcg_congestion(struct pglist_data *pgdat,
  				struct mem_cgroup *memcg, bool congested)
  {
  }
  
  static inline bool memcg_congested(struct pglist_data *pgdat,
  			struct mem_cgroup *memcg)
  {
  	return false;
  
  }
91a45470f   KAMEZAWA Hiroyuki   per-zone and recl...
311
  #endif
5a1c84b40   Mel Gorman   mm: remove reclai...
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
  /*
   * This misses isolated pages which are not accounted for to save counters.
   * As the data only determines if reclaim or compaction continues, it is
   * not expected that isolated pages will be a dominating factor.
   */
  unsigned long zone_reclaimable_pages(struct zone *zone)
  {
  	unsigned long nr;
  
  	nr = zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_FILE) +
  		zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_FILE);
  	if (get_nr_swap_pages() > 0)
  		nr += zone_page_state_snapshot(zone, NR_ZONE_INACTIVE_ANON) +
  			zone_page_state_snapshot(zone, NR_ZONE_ACTIVE_ANON);
  
  	return nr;
  }
fd5388037   Michal Hocko   mm, vmscan: clean...
329
330
331
332
333
334
335
  /**
   * lruvec_lru_size -  Returns the number of pages on the given LRU list.
   * @lruvec: lru vector
   * @lru: lru to use
   * @zone_idx: zones to consider (use MAX_NR_ZONES for the whole LRU list)
   */
  unsigned long lruvec_lru_size(struct lruvec *lruvec, enum lru_list lru, int zone_idx)
c9f299d98   KOSAKI Motohiro   mm: add zone nr_p...
336
  {
b11edebbc   Honglei Wang   mm: memcg: get nu...
337
  	unsigned long lru_size = 0;
fd5388037   Michal Hocko   mm, vmscan: clean...
338
  	int zid;
b11edebbc   Honglei Wang   mm: memcg: get nu...
339
340
341
342
  	if (!mem_cgroup_disabled()) {
  		for (zid = 0; zid < MAX_NR_ZONES; zid++)
  			lru_size += mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
  	} else
fd5388037   Michal Hocko   mm, vmscan: clean...
343
  		lru_size = node_page_state(lruvec_pgdat(lruvec), NR_LRU_BASE + lru);
a3d8e0549   KOSAKI Motohiro   memcg: add mem_cg...
344

fd5388037   Michal Hocko   mm, vmscan: clean...
345
346
347
  	for (zid = zone_idx + 1; zid < MAX_NR_ZONES; zid++) {
  		struct zone *zone = &lruvec_pgdat(lruvec)->node_zones[zid];
  		unsigned long size;
c9f299d98   KOSAKI Motohiro   mm: add zone nr_p...
348

fd5388037   Michal Hocko   mm, vmscan: clean...
349
350
351
352
353
354
355
356
357
358
359
360
  		if (!managed_zone(zone))
  			continue;
  
  		if (!mem_cgroup_disabled())
  			size = mem_cgroup_get_zone_lru_size(lruvec, lru, zid);
  		else
  			size = zone_page_state(&lruvec_pgdat(lruvec)->node_zones[zid],
  				       NR_ZONE_LRU_BASE + lru);
  		lru_size -= min(size, lru_size);
  	}
  
  	return lru_size;
b4536f0c8   Michal Hocko   mm, memcg: fix th...
361

b4536f0c8   Michal Hocko   mm, memcg: fix th...
362
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
363
  /*
1d3d4437e   Glauber Costa   vmscan: per-node ...
364
   * Add a shrinker callback to be called from the vm.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
   */
8e04944f0   Tetsuo Handa   mm,vmscan: Allow ...
366
  int prealloc_shrinker(struct shrinker *shrinker)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
  {
b9726c26d   Alexey Dobriyan   numa: make "nr_no...
368
  	unsigned int size = sizeof(*shrinker->nr_deferred);
1d3d4437e   Glauber Costa   vmscan: per-node ...
369

1d3d4437e   Glauber Costa   vmscan: per-node ...
370
371
372
373
374
375
  	if (shrinker->flags & SHRINKER_NUMA_AWARE)
  		size *= nr_node_ids;
  
  	shrinker->nr_deferred = kzalloc(size, GFP_KERNEL);
  	if (!shrinker->nr_deferred)
  		return -ENOMEM;
b4c2b231c   Kirill Tkhai   mm: assign id to ...
376
377
378
379
380
  
  	if (shrinker->flags & SHRINKER_MEMCG_AWARE) {
  		if (prealloc_memcg_shrinker(shrinker))
  			goto free_deferred;
  	}
8e04944f0   Tetsuo Handa   mm,vmscan: Allow ...
381
  	return 0;
b4c2b231c   Kirill Tkhai   mm: assign id to ...
382
383
384
385
386
  
  free_deferred:
  	kfree(shrinker->nr_deferred);
  	shrinker->nr_deferred = NULL;
  	return -ENOMEM;
8e04944f0   Tetsuo Handa   mm,vmscan: Allow ...
387
388
389
390
  }
  
  void free_prealloced_shrinker(struct shrinker *shrinker)
  {
b4c2b231c   Kirill Tkhai   mm: assign id to ...
391
392
393
394
395
  	if (!shrinker->nr_deferred)
  		return;
  
  	if (shrinker->flags & SHRINKER_MEMCG_AWARE)
  		unregister_memcg_shrinker(shrinker);
8e04944f0   Tetsuo Handa   mm,vmscan: Allow ...
396
397
398
  	kfree(shrinker->nr_deferred);
  	shrinker->nr_deferred = NULL;
  }
1d3d4437e   Glauber Costa   vmscan: per-node ...
399

8e04944f0   Tetsuo Handa   mm,vmscan: Allow ...
400
401
  void register_shrinker_prepared(struct shrinker *shrinker)
  {
8e1f936b7   Rusty Russell   mm: clean up and ...
402
403
  	down_write(&shrinker_rwsem);
  	list_add_tail(&shrinker->list, &shrinker_list);
b58b8e933   Yang Shi   mm: vmscan: prote...
404
  #ifdef CONFIG_MEMCG
8df4a44cc   Kirill Tkhai   mm: check shrinke...
405
406
  	if (shrinker->flags & SHRINKER_MEMCG_AWARE)
  		idr_replace(&shrinker_idr, shrinker, shrinker->id);
7e010df53   Kirill Tkhai   mm: use special v...
407
  #endif
8e1f936b7   Rusty Russell   mm: clean up and ...
408
  	up_write(&shrinker_rwsem);
8e04944f0   Tetsuo Handa   mm,vmscan: Allow ...
409
410
411
412
413
414
415
416
417
  }
  
  int register_shrinker(struct shrinker *shrinker)
  {
  	int err = prealloc_shrinker(shrinker);
  
  	if (err)
  		return err;
  	register_shrinker_prepared(shrinker);
1d3d4437e   Glauber Costa   vmscan: per-node ...
418
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419
  }
8e1f936b7   Rusty Russell   mm: clean up and ...
420
  EXPORT_SYMBOL(register_shrinker);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
421
422
423
424
  
  /*
   * Remove one
   */
8e1f936b7   Rusty Russell   mm: clean up and ...
425
  void unregister_shrinker(struct shrinker *shrinker)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426
  {
bb422a738   Tetsuo Handa   mm,vmscan: Make u...
427
428
  	if (!shrinker->nr_deferred)
  		return;
b4c2b231c   Kirill Tkhai   mm: assign id to ...
429
430
  	if (shrinker->flags & SHRINKER_MEMCG_AWARE)
  		unregister_memcg_shrinker(shrinker);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
432
433
  	down_write(&shrinker_rwsem);
  	list_del(&shrinker->list);
  	up_write(&shrinker_rwsem);
ae3933216   Andrew Vagin   mm/vmscan.c: don'...
434
  	kfree(shrinker->nr_deferred);
bb422a738   Tetsuo Handa   mm,vmscan: Make u...
435
  	shrinker->nr_deferred = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
  }
8e1f936b7   Rusty Russell   mm: clean up and ...
437
  EXPORT_SYMBOL(unregister_shrinker);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
439
  
  #define SHRINK_BATCH 128
1d3d4437e   Glauber Costa   vmscan: per-node ...
440

cb731d6c6   Vladimir Davydov   vmscan: per memor...
441
  static unsigned long do_shrink_slab(struct shrink_control *shrinkctl,
9092c71bb   Josef Bacik   mm: use sc->prior...
442
  				    struct shrinker *shrinker, int priority)
1d3d4437e   Glauber Costa   vmscan: per-node ...
443
444
445
446
  {
  	unsigned long freed = 0;
  	unsigned long long delta;
  	long total_scan;
d5bc5fd3f   Vladimir Davydov   mm: vmscan: shrin...
447
  	long freeable;
1d3d4437e   Glauber Costa   vmscan: per-node ...
448
449
450
451
452
  	long nr;
  	long new_nr;
  	int nid = shrinkctl->nid;
  	long batch_size = shrinker->batch ? shrinker->batch
  					  : SHRINK_BATCH;
5f33a0803   Shaohua Li   mm/vmscan.c: set ...
453
  	long scanned = 0, next_deferred;
1d3d4437e   Glauber Costa   vmscan: per-node ...
454

ac7fb3ad2   Kirill Tkhai   mm/vmscan.c: move...
455
456
  	if (!(shrinker->flags & SHRINKER_NUMA_AWARE))
  		nid = 0;
d5bc5fd3f   Vladimir Davydov   mm: vmscan: shrin...
457
  	freeable = shrinker->count_objects(shrinker, shrinkctl);
9b996468c   Kirill Tkhai   mm: add SHRINK_EM...
458
459
  	if (freeable == 0 || freeable == SHRINK_EMPTY)
  		return freeable;
1d3d4437e   Glauber Costa   vmscan: per-node ...
460
461
462
463
464
465
466
467
468
  
  	/*
  	 * copy the current shrinker scan count into a local variable
  	 * and zero it so that other concurrent shrinker invocations
  	 * don't also do this scanning work.
  	 */
  	nr = atomic_long_xchg(&shrinker->nr_deferred[nid], 0);
  
  	total_scan = nr;
4b85afbda   Johannes Weiner   mm: zero-seek shr...
469
470
471
472
473
474
475
476
477
478
479
480
  	if (shrinker->seeks) {
  		delta = freeable >> priority;
  		delta *= 4;
  		do_div(delta, shrinker->seeks);
  	} else {
  		/*
  		 * These objects don't require any IO to create. Trim
  		 * them aggressively under memory pressure to keep
  		 * them from causing refetches in the IO caches.
  		 */
  		delta = freeable / 2;
  	}
172b06c32   Roman Gushchin   mm: slowly shrink...
481

1d3d4437e   Glauber Costa   vmscan: per-node ...
482
483
  	total_scan += delta;
  	if (total_scan < 0) {
d75f773c8   Sakari Ailus   treewide: Switch ...
484
485
  		pr_err("shrink_slab: %pS negative objects to delete nr=%ld
  ",
a0b02131c   Dave Chinner   shrinker: Kill ol...
486
  		       shrinker->scan_objects, total_scan);
d5bc5fd3f   Vladimir Davydov   mm: vmscan: shrin...
487
  		total_scan = freeable;
5f33a0803   Shaohua Li   mm/vmscan.c: set ...
488
489
490
  		next_deferred = nr;
  	} else
  		next_deferred = total_scan;
1d3d4437e   Glauber Costa   vmscan: per-node ...
491
492
493
494
495
496
497
  
  	/*
  	 * We need to avoid excessive windup on filesystem shrinkers
  	 * due to large numbers of GFP_NOFS allocations causing the
  	 * shrinkers to return -1 all the time. This results in a large
  	 * nr being built up so when a shrink that can do some work
  	 * comes along it empties the entire cache due to nr >>>
d5bc5fd3f   Vladimir Davydov   mm: vmscan: shrin...
498
  	 * freeable. This is bad for sustaining a working set in
1d3d4437e   Glauber Costa   vmscan: per-node ...
499
500
501
502
503
  	 * memory.
  	 *
  	 * Hence only allow the shrinker to scan the entire cache when
  	 * a large delta change is calculated directly.
  	 */
d5bc5fd3f   Vladimir Davydov   mm: vmscan: shrin...
504
505
  	if (delta < freeable / 4)
  		total_scan = min(total_scan, freeable / 2);
1d3d4437e   Glauber Costa   vmscan: per-node ...
506
507
508
509
510
511
  
  	/*
  	 * Avoid risking looping forever due to too large nr value:
  	 * never try to free more than twice the estimate number of
  	 * freeable entries.
  	 */
d5bc5fd3f   Vladimir Davydov   mm: vmscan: shrin...
512
513
  	if (total_scan > freeable * 2)
  		total_scan = freeable * 2;
1d3d4437e   Glauber Costa   vmscan: per-node ...
514
515
  
  	trace_mm_shrink_slab_start(shrinker, shrinkctl, nr,
9092c71bb   Josef Bacik   mm: use sc->prior...
516
  				   freeable, delta, total_scan, priority);
1d3d4437e   Glauber Costa   vmscan: per-node ...
517

0b1fb40a3   Vladimir Davydov   mm: vmscan: shrin...
518
519
520
521
522
523
524
525
526
527
528
  	/*
  	 * Normally, we should not scan less than batch_size objects in one
  	 * pass to avoid too frequent shrinker calls, but if the slab has less
  	 * than batch_size objects in total and we are really tight on memory,
  	 * we will try to reclaim all available objects, otherwise we can end
  	 * up failing allocations although there are plenty of reclaimable
  	 * objects spread over several slabs with usage less than the
  	 * batch_size.
  	 *
  	 * We detect the "tight on memory" situations by looking at the total
  	 * number of objects we want to scan (total_scan). If it is greater
d5bc5fd3f   Vladimir Davydov   mm: vmscan: shrin...
529
  	 * than the total number of objects on slab (freeable), we must be
0b1fb40a3   Vladimir Davydov   mm: vmscan: shrin...
530
531
532
533
  	 * scanning at high prio and therefore should try to reclaim as much as
  	 * possible.
  	 */
  	while (total_scan >= batch_size ||
d5bc5fd3f   Vladimir Davydov   mm: vmscan: shrin...
534
  	       total_scan >= freeable) {
a0b02131c   Dave Chinner   shrinker: Kill ol...
535
  		unsigned long ret;
0b1fb40a3   Vladimir Davydov   mm: vmscan: shrin...
536
  		unsigned long nr_to_scan = min(batch_size, total_scan);
1d3d4437e   Glauber Costa   vmscan: per-node ...
537

0b1fb40a3   Vladimir Davydov   mm: vmscan: shrin...
538
  		shrinkctl->nr_to_scan = nr_to_scan;
d460acb5b   Chris Wilson   mm: track actual ...
539
  		shrinkctl->nr_scanned = nr_to_scan;
a0b02131c   Dave Chinner   shrinker: Kill ol...
540
541
542
543
  		ret = shrinker->scan_objects(shrinker, shrinkctl);
  		if (ret == SHRINK_STOP)
  			break;
  		freed += ret;
1d3d4437e   Glauber Costa   vmscan: per-node ...
544

d460acb5b   Chris Wilson   mm: track actual ...
545
546
547
  		count_vm_events(SLABS_SCANNED, shrinkctl->nr_scanned);
  		total_scan -= shrinkctl->nr_scanned;
  		scanned += shrinkctl->nr_scanned;
1d3d4437e   Glauber Costa   vmscan: per-node ...
548
549
550
  
  		cond_resched();
  	}
5f33a0803   Shaohua Li   mm/vmscan.c: set ...
551
552
553
554
  	if (next_deferred >= scanned)
  		next_deferred -= scanned;
  	else
  		next_deferred = 0;
1d3d4437e   Glauber Costa   vmscan: per-node ...
555
556
557
558
559
  	/*
  	 * move the unused scan count back into the shrinker in a
  	 * manner that handles concurrent updates. If we exhausted the
  	 * scan, there is no need to do an update.
  	 */
5f33a0803   Shaohua Li   mm/vmscan.c: set ...
560
561
  	if (next_deferred > 0)
  		new_nr = atomic_long_add_return(next_deferred,
1d3d4437e   Glauber Costa   vmscan: per-node ...
562
563
564
  						&shrinker->nr_deferred[nid]);
  	else
  		new_nr = atomic_long_read(&shrinker->nr_deferred[nid]);
df9024a8c   Dave Hansen   mm: shrinker: add...
565
  	trace_mm_shrink_slab_end(shrinker, nid, freed, nr, new_nr, total_scan);
1d3d4437e   Glauber Costa   vmscan: per-node ...
566
  	return freed;
1495f230f   Ying Han   vmscan: change sh...
567
  }
0a432dcbe   Yang Shi   mm: shrinker: mak...
568
  #ifdef CONFIG_MEMCG
b0dedc49a   Kirill Tkhai   mm/vmscan.c: iter...
569
570
571
572
  static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
  			struct mem_cgroup *memcg, int priority)
  {
  	struct memcg_shrinker_map *map;
b8e57efa2   Kirill Tkhai   mm/vmscan.c: fix ...
573
574
  	unsigned long ret, freed = 0;
  	int i;
b0dedc49a   Kirill Tkhai   mm/vmscan.c: iter...
575

0a432dcbe   Yang Shi   mm: shrinker: mak...
576
  	if (!mem_cgroup_online(memcg))
b0dedc49a   Kirill Tkhai   mm/vmscan.c: iter...
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  		return 0;
  
  	if (!down_read_trylock(&shrinker_rwsem))
  		return 0;
  
  	map = rcu_dereference_protected(memcg->nodeinfo[nid]->shrinker_map,
  					true);
  	if (unlikely(!map))
  		goto unlock;
  
  	for_each_set_bit(i, map->map, shrinker_nr_max) {
  		struct shrink_control sc = {
  			.gfp_mask = gfp_mask,
  			.nid = nid,
  			.memcg = memcg,
  		};
  		struct shrinker *shrinker;
  
  		shrinker = idr_find(&shrinker_idr, i);
7e010df53   Kirill Tkhai   mm: use special v...
596
597
598
  		if (unlikely(!shrinker || shrinker == SHRINKER_REGISTERING)) {
  			if (!shrinker)
  				clear_bit(i, map->map);
b0dedc49a   Kirill Tkhai   mm/vmscan.c: iter...
599
600
  			continue;
  		}
0a432dcbe   Yang Shi   mm: shrinker: mak...
601
602
603
604
  		/* Call non-slab shrinkers even though kmem is disabled */
  		if (!memcg_kmem_enabled() &&
  		    !(shrinker->flags & SHRINKER_NONSLAB))
  			continue;
b0dedc49a   Kirill Tkhai   mm/vmscan.c: iter...
605
  		ret = do_shrink_slab(&sc, shrinker, priority);
f90280d6b   Kirill Tkhai   mm/vmscan.c: clea...
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
  		if (ret == SHRINK_EMPTY) {
  			clear_bit(i, map->map);
  			/*
  			 * After the shrinker reported that it had no objects to
  			 * free, but before we cleared the corresponding bit in
  			 * the memcg shrinker map, a new object might have been
  			 * added. To make sure, we have the bit set in this
  			 * case, we invoke the shrinker one more time and reset
  			 * the bit if it reports that it is not empty anymore.
  			 * The memory barrier here pairs with the barrier in
  			 * memcg_set_shrinker_bit():
  			 *
  			 * list_lru_add()     shrink_slab_memcg()
  			 *   list_add_tail()    clear_bit()
  			 *   <MB>               <MB>
  			 *   set_bit()          do_shrink_slab()
  			 */
  			smp_mb__after_atomic();
  			ret = do_shrink_slab(&sc, shrinker, priority);
  			if (ret == SHRINK_EMPTY)
  				ret = 0;
  			else
  				memcg_set_shrinker_bit(memcg, nid, i);
  		}
b0dedc49a   Kirill Tkhai   mm/vmscan.c: iter...
630
631
632
633
634
635
636
637
638
639
640
  		freed += ret;
  
  		if (rwsem_is_contended(&shrinker_rwsem)) {
  			freed = freed ? : 1;
  			break;
  		}
  	}
  unlock:
  	up_read(&shrinker_rwsem);
  	return freed;
  }
0a432dcbe   Yang Shi   mm: shrinker: mak...
641
  #else /* CONFIG_MEMCG */
b0dedc49a   Kirill Tkhai   mm/vmscan.c: iter...
642
643
644
645
646
  static unsigned long shrink_slab_memcg(gfp_t gfp_mask, int nid,
  			struct mem_cgroup *memcg, int priority)
  {
  	return 0;
  }
0a432dcbe   Yang Shi   mm: shrinker: mak...
647
  #endif /* CONFIG_MEMCG */
b0dedc49a   Kirill Tkhai   mm/vmscan.c: iter...
648

6b4f7799c   Johannes Weiner   mm: vmscan: invok...
649
  /**
cb731d6c6   Vladimir Davydov   vmscan: per memor...
650
   * shrink_slab - shrink slab caches
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
651
652
   * @gfp_mask: allocation context
   * @nid: node whose slab caches to target
cb731d6c6   Vladimir Davydov   vmscan: per memor...
653
   * @memcg: memory cgroup whose slab caches to target
9092c71bb   Josef Bacik   mm: use sc->prior...
654
   * @priority: the reclaim priority
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
   *
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
656
   * Call the shrink functions to age shrinkable caches.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
   *
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
658
659
   * @nid is passed along to shrinkers with SHRINKER_NUMA_AWARE set,
   * unaware shrinkers will receive a node id of 0 instead.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
   *
aeed1d325   Vladimir Davydov   mm/vmscan.c: gene...
661
662
   * @memcg specifies the memory cgroup to target. Unaware shrinkers
   * are called only if it is the root cgroup.
cb731d6c6   Vladimir Davydov   vmscan: per memor...
663
   *
9092c71bb   Josef Bacik   mm: use sc->prior...
664
665
   * @priority is sc->priority, we take the number of objects and >> by priority
   * in order to get the scan target.
b15e0905f   Andrew Morton   [PATCH] vmscan: n...
666
   *
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
667
   * Returns the number of reclaimed slab objects.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
   */
cb731d6c6   Vladimir Davydov   vmscan: per memor...
669
670
  static unsigned long shrink_slab(gfp_t gfp_mask, int nid,
  				 struct mem_cgroup *memcg,
9092c71bb   Josef Bacik   mm: use sc->prior...
671
  				 int priority)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
672
  {
b8e57efa2   Kirill Tkhai   mm/vmscan.c: fix ...
673
  	unsigned long ret, freed = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
674
  	struct shrinker *shrinker;
fa1e512fa   Yang Shi   mm: vmscan: check...
675
676
677
678
679
680
681
682
  	/*
  	 * The root memcg might be allocated even though memcg is disabled
  	 * via "cgroup_disable=memory" boot parameter.  This could make
  	 * mem_cgroup_is_root() return false, then just run memcg slab
  	 * shrink, but skip global shrink.  This may result in premature
  	 * oom.
  	 */
  	if (!mem_cgroup_disabled() && !mem_cgroup_is_root(memcg))
b0dedc49a   Kirill Tkhai   mm/vmscan.c: iter...
683
  		return shrink_slab_memcg(gfp_mask, nid, memcg, priority);
cb731d6c6   Vladimir Davydov   vmscan: per memor...
684

e830c63a6   Tetsuo Handa   mm,vmscan: don't ...
685
  	if (!down_read_trylock(&shrinker_rwsem))
f06590bd7   Minchan Kim   mm: vmscan: corre...
686
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
688
  
  	list_for_each_entry(shrinker, &shrinker_list, list) {
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
689
690
691
  		struct shrink_control sc = {
  			.gfp_mask = gfp_mask,
  			.nid = nid,
cb731d6c6   Vladimir Davydov   vmscan: per memor...
692
  			.memcg = memcg,
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
693
  		};
ec97097bc   Vladimir Davydov   mm: vmscan: call ...
694

9b996468c   Kirill Tkhai   mm: add SHRINK_EM...
695
696
697
698
  		ret = do_shrink_slab(&sc, shrinker, priority);
  		if (ret == SHRINK_EMPTY)
  			ret = 0;
  		freed += ret;
e496612c5   Minchan Kim   mm: do not stall ...
699
700
701
702
703
704
705
706
707
  		/*
  		 * Bail out if someone want to register a new shrinker to
  		 * prevent the regsitration from being stalled for long periods
  		 * by parallel ongoing shrinking.
  		 */
  		if (rwsem_is_contended(&shrinker_rwsem)) {
  			freed = freed ? : 1;
  			break;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
708
  	}
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
709

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
710
  	up_read(&shrinker_rwsem);
f06590bd7   Minchan Kim   mm: vmscan: corre...
711
712
  out:
  	cond_resched();
24f7c6b98   Dave Chinner   mm: new shrinker API
713
  	return freed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
714
  }
cb731d6c6   Vladimir Davydov   vmscan: per memor...
715
716
717
718
719
720
721
722
  void drop_slab_node(int nid)
  {
  	unsigned long freed;
  
  	do {
  		struct mem_cgroup *memcg = NULL;
  
  		freed = 0;
aeed1d325   Vladimir Davydov   mm/vmscan.c: gene...
723
  		memcg = mem_cgroup_iter(NULL, NULL, NULL);
cb731d6c6   Vladimir Davydov   vmscan: per memor...
724
  		do {
9092c71bb   Josef Bacik   mm: use sc->prior...
725
  			freed += shrink_slab(GFP_KERNEL, nid, memcg, 0);
cb731d6c6   Vladimir Davydov   vmscan: per memor...
726
727
728
729
730
731
732
733
734
735
736
  		} while ((memcg = mem_cgroup_iter(NULL, memcg, NULL)) != NULL);
  	} while (freed > 10);
  }
  
  void drop_slab(void)
  {
  	int nid;
  
  	for_each_online_node(nid)
  		drop_slab_node(nid);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
737
738
  static inline int is_page_cache_freeable(struct page *page)
  {
ceddc3a52   Johannes Weiner   mm: document is_p...
739
740
  	/*
  	 * A freeable page cache page is referenced only by the caller
67891ffff   Matthew Wilcox   mm: Convert is_pa...
741
742
  	 * that isolated the page, the page cache and optional buffer
  	 * heads at page->private.
ceddc3a52   Johannes Weiner   mm: document is_p...
743
  	 */
67891ffff   Matthew Wilcox   mm: Convert is_pa...
744
  	int page_cache_pins = PageTransHuge(page) && PageSwapCache(page) ?
bd4c82c22   Huang Ying   mm, THP, swap: de...
745
  		HPAGE_PMD_NR : 1;
67891ffff   Matthew Wilcox   mm: Convert is_pa...
746
  	return page_count(page) - page_has_private(page) == 1 + page_cache_pins;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
747
  }
703c27088   Tejun Heo   writeback: implem...
748
  static int may_write_to_inode(struct inode *inode, struct scan_control *sc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
749
  {
930d91525   Christoph Lameter   [PATCH] Swap Migr...
750
  	if (current->flags & PF_SWAPWRITE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751
  		return 1;
703c27088   Tejun Heo   writeback: implem...
752
  	if (!inode_write_congested(inode))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
753
  		return 1;
703c27088   Tejun Heo   writeback: implem...
754
  	if (inode_to_bdi(inode) == current->backing_dev_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
  		return 1;
  	return 0;
  }
  
  /*
   * We detected a synchronous write error writing a page out.  Probably
   * -ENOSPC.  We need to propagate that into the address_space for a subsequent
   * fsync(), msync() or close().
   *
   * The tricky part is that after writepage we cannot touch the mapping: nothing
   * prevents it from being freed up.  But we have a ref on the page and once
   * that page is locked, the mapping is pinned.
   *
   * We're allowed to run sleeping lock_page() here because we know the caller has
   * __GFP_FS.
   */
  static void handle_write_error(struct address_space *mapping,
  				struct page *page, int error)
  {
7eaceacca   Jens Axboe   block: remove per...
774
  	lock_page(page);
3e9f45bd1   Guillaume Chazarain   Factor outstandin...
775
776
  	if (page_mapping(page) == mapping)
  		mapping_set_error(mapping, error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
777
778
  	unlock_page(page);
  }
04e62a29b   Christoph Lameter   [PATCH] More page...
779
780
781
782
783
784
785
786
787
788
789
  /* possible outcome of pageout() */
  typedef enum {
  	/* failed to write page out, page is locked */
  	PAGE_KEEP,
  	/* move page to the active list, page is locked */
  	PAGE_ACTIVATE,
  	/* page has been sent to the disk successfully, page is unlocked */
  	PAGE_SUCCESS,
  	/* page is clean and locked */
  	PAGE_CLEAN,
  } pageout_t;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
790
  /*
1742f19fa   Andrew Morton   [PATCH] vmscan: r...
791
792
   * pageout is called by shrink_page_list() for each dirty page.
   * Calls ->writepage().
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
793
   */
c661b078f   Andy Whitcroft   synchronous lumpy...
794
  static pageout_t pageout(struct page *page, struct address_space *mapping,
7d3579e8e   KOSAKI Motohiro   vmscan: narrow th...
795
  			 struct scan_control *sc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
796
797
798
799
800
801
802
803
  {
  	/*
  	 * If the page is dirty, only perform writeback if that write
  	 * will be non-blocking.  To prevent this allocation from being
  	 * stalled by pagecache activity.  But note that there may be
  	 * stalls if we need to run get_block().  We could test
  	 * PagePrivate for that.
  	 *
8174202b3   Al Viro   write_iter varian...
804
  	 * If this process is currently in __generic_file_write_iter() against
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
805
806
807
808
809
810
811
  	 * this page's queue, we can perform writeback even if that
  	 * will block.
  	 *
  	 * If the page is swapcache, write it back even if that would
  	 * block, for some throttling. This happens by accident, because
  	 * swap_backing_dev_info is bust: it doesn't reflect the
  	 * congestion state of the swapdevs.  Easy to fix, if needed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
812
813
814
815
816
817
818
819
  	 */
  	if (!is_page_cache_freeable(page))
  		return PAGE_KEEP;
  	if (!mapping) {
  		/*
  		 * Some data journaling orphaned pages can have
  		 * page->mapping == NULL while being dirty with clean buffers.
  		 */
266cf658e   David Howells   FS-Cache: Recruit...
820
  		if (page_has_private(page)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
821
822
  			if (try_to_free_buffers(page)) {
  				ClearPageDirty(page);
b1de0d139   Mitchel Humpherys   mm: convert some ...
823
824
  				pr_info("%s: orphaned page
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
825
826
827
828
829
830
831
  				return PAGE_CLEAN;
  			}
  		}
  		return PAGE_KEEP;
  	}
  	if (mapping->a_ops->writepage == NULL)
  		return PAGE_ACTIVATE;
703c27088   Tejun Heo   writeback: implem...
832
  	if (!may_write_to_inode(mapping->host, sc))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
833
834
835
836
837
838
839
  		return PAGE_KEEP;
  
  	if (clear_page_dirty_for_io(page)) {
  		int res;
  		struct writeback_control wbc = {
  			.sync_mode = WB_SYNC_NONE,
  			.nr_to_write = SWAP_CLUSTER_MAX,
111ebb6e6   OGAWA Hirofumi   [PATCH] writeback...
840
841
  			.range_start = 0,
  			.range_end = LLONG_MAX,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842
843
844
845
846
847
848
  			.for_reclaim = 1,
  		};
  
  		SetPageReclaim(page);
  		res = mapping->a_ops->writepage(page, &wbc);
  		if (res < 0)
  			handle_write_error(mapping, page, res);
994fc28c7   Zach Brown   [PATCH] add AOP_T...
849
  		if (res == AOP_WRITEPAGE_ACTIVATE) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
851
852
  			ClearPageReclaim(page);
  			return PAGE_ACTIVATE;
  		}
c661b078f   Andy Whitcroft   synchronous lumpy...
853

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
854
855
856
857
  		if (!PageWriteback(page)) {
  			/* synchronous write or broken a_ops? */
  			ClearPageReclaim(page);
  		}
3aa238511   yalin wang   mm/vmscan.c: chan...
858
  		trace_mm_vmscan_writepage(page);
c4a25635b   Mel Gorman   mm: move vmscan w...
859
  		inc_node_page_state(page, NR_VMSCAN_WRITE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
860
861
862
863
864
  		return PAGE_SUCCESS;
  	}
  
  	return PAGE_CLEAN;
  }
a649fd927   Andrew Morton   [PATCH] invalidat...
865
  /*
e286781d5   Nick Piggin   mm: speculative p...
866
867
   * Same as remove_mapping, but if the page is removed from the mapping, it
   * gets returned with a refcount of 0.
a649fd927   Andrew Morton   [PATCH] invalidat...
868
   */
a528910e1   Johannes Weiner   mm: thrash detect...
869
870
  static int __remove_mapping(struct address_space *mapping, struct page *page,
  			    bool reclaimed)
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
871
  {
c4843a759   Greg Thelen   memcg: add per cg...
872
  	unsigned long flags;
bd4c82c22   Huang Ying   mm, THP, swap: de...
873
  	int refcount;
c4843a759   Greg Thelen   memcg: add per cg...
874

28e4d965e   Nick Piggin   [PATCH] mm: remov...
875
876
  	BUG_ON(!PageLocked(page));
  	BUG_ON(mapping != page_mapping(page));
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
877

b93b01631   Matthew Wilcox   page cache: use x...
878
  	xa_lock_irqsave(&mapping->i_pages, flags);
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
879
  	/*
0fd0e6b05   Nick Piggin   [PATCH] page inva...
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
  	 * The non racy check for a busy page.
  	 *
  	 * Must be careful with the order of the tests. When someone has
  	 * a ref to the page, it may be possible that they dirty it then
  	 * drop the reference. So if PageDirty is tested before page_count
  	 * here, then the following race may occur:
  	 *
  	 * get_user_pages(&page);
  	 * [user mapping goes away]
  	 * write_to(page);
  	 *				!PageDirty(page)    [good]
  	 * SetPageDirty(page);
  	 * put_page(page);
  	 *				!page_count(page)   [good, discard it]
  	 *
  	 * [oops, our write_to data is lost]
  	 *
  	 * Reversing the order of the tests ensures such a situation cannot
  	 * escape unnoticed. The smp_rmb is needed to ensure the page->flags
0139aa7b7   Joonsoo Kim   mm: rename _count...
899
  	 * load is not satisfied before that of page->_refcount.
0fd0e6b05   Nick Piggin   [PATCH] page inva...
900
901
  	 *
  	 * Note that if SetPageDirty is always performed via set_page_dirty,
b93b01631   Matthew Wilcox   page cache: use x...
902
  	 * and thus under the i_pages lock, then this ordering is not required.
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
903
  	 */
906d278d7   William Kucharski   mm/vmscan.c: supp...
904
  	refcount = 1 + compound_nr(page);
bd4c82c22   Huang Ying   mm, THP, swap: de...
905
  	if (!page_ref_freeze(page, refcount))
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
906
  		goto cannot_free;
1c4c3b99c   Jiang Biao   mm: fix page_free...
907
  	/* note: atomic_cmpxchg in page_ref_freeze provides the smp_rmb */
e286781d5   Nick Piggin   mm: speculative p...
908
  	if (unlikely(PageDirty(page))) {
bd4c82c22   Huang Ying   mm, THP, swap: de...
909
  		page_ref_unfreeze(page, refcount);
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
910
  		goto cannot_free;
e286781d5   Nick Piggin   mm: speculative p...
911
  	}
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
912
913
914
  
  	if (PageSwapCache(page)) {
  		swp_entry_t swap = { .val = page_private(page) };
0a31bc97c   Johannes Weiner   mm: memcontrol: r...
915
  		mem_cgroup_swapout(page, swap);
4e17ec250   Matthew Wilcox   mm: Convert delet...
916
  		__delete_from_swap_cache(page, swap);
b93b01631   Matthew Wilcox   page cache: use x...
917
  		xa_unlock_irqrestore(&mapping->i_pages, flags);
75f6d6d29   Minchan Kim   mm, THP, swap: un...
918
  		put_swap_page(page, swap);
e286781d5   Nick Piggin   mm: speculative p...
919
  	} else {
6072d13c4   Linus Torvalds   Call the filesyst...
920
  		void (*freepage)(struct page *);
a528910e1   Johannes Weiner   mm: thrash detect...
921
  		void *shadow = NULL;
6072d13c4   Linus Torvalds   Call the filesyst...
922
923
  
  		freepage = mapping->a_ops->freepage;
a528910e1   Johannes Weiner   mm: thrash detect...
924
925
926
927
928
929
930
931
932
  		/*
  		 * Remember a shadow entry for reclaimed file cache in
  		 * order to detect refaults, thus thrashing, later on.
  		 *
  		 * But don't store shadows in an address space that is
  		 * already exiting.  This is not just an optizimation,
  		 * inode reclaim needs to empty out the radix tree or
  		 * the nodes are lost.  Don't plant shadows behind its
  		 * back.
f9fe48bec   Ross Zwisler   dax: support dirt...
933
934
935
936
937
  		 *
  		 * We also don't store shadows for DAX mappings because the
  		 * only page cache pages found in these are zero pages
  		 * covering holes, and because we don't want to mix DAX
  		 * exceptional entries and shadow exceptional entries in the
b93b01631   Matthew Wilcox   page cache: use x...
938
  		 * same address_space.
a528910e1   Johannes Weiner   mm: thrash detect...
939
940
  		 */
  		if (reclaimed && page_is_file_cache(page) &&
f9fe48bec   Ross Zwisler   dax: support dirt...
941
  		    !mapping_exiting(mapping) && !dax_mapping(mapping))
a7ca12f9d   Andrey Ryabinin   mm/workingset: re...
942
  			shadow = workingset_eviction(page);
62cccb8c8   Johannes Weiner   mm: simplify lock...
943
  		__delete_from_page_cache(page, shadow);
b93b01631   Matthew Wilcox   page cache: use x...
944
  		xa_unlock_irqrestore(&mapping->i_pages, flags);
6072d13c4   Linus Torvalds   Call the filesyst...
945
946
947
  
  		if (freepage != NULL)
  			freepage(page);
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
948
  	}
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
949
950
951
  	return 1;
  
  cannot_free:
b93b01631   Matthew Wilcox   page cache: use x...
952
  	xa_unlock_irqrestore(&mapping->i_pages, flags);
49d2e9cc4   Christoph Lameter   [PATCH] Swap Migr...
953
954
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
955
  /*
e286781d5   Nick Piggin   mm: speculative p...
956
957
958
959
960
961
962
   * Attempt to detach a locked page from its ->mapping.  If it is dirty or if
   * someone else has a ref on the page, abort and return 0.  If it was
   * successfully detached, return 1.  Assumes the caller has a single ref on
   * this page.
   */
  int remove_mapping(struct address_space *mapping, struct page *page)
  {
a528910e1   Johannes Weiner   mm: thrash detect...
963
  	if (__remove_mapping(mapping, page, false)) {
e286781d5   Nick Piggin   mm: speculative p...
964
965
966
967
968
  		/*
  		 * Unfreezing the refcount with 1 rather than 2 effectively
  		 * drops the pagecache ref for us without requiring another
  		 * atomic operation.
  		 */
fe896d187   Joonsoo Kim   mm: introduce pag...
969
  		page_ref_unfreeze(page, 1);
e286781d5   Nick Piggin   mm: speculative p...
970
971
972
973
  		return 1;
  	}
  	return 0;
  }
894bc3104   Lee Schermerhorn   Unevictable LRU I...
974
975
976
977
978
979
980
981
982
  /**
   * putback_lru_page - put previously isolated page onto appropriate LRU list
   * @page: page to be put back to appropriate lru list
   *
   * Add previously isolated @page to appropriate LRU list.
   * Page may still be unevictable for other reasons.
   *
   * lru_lock must not be held, interrupts must be enabled.
   */
894bc3104   Lee Schermerhorn   Unevictable LRU I...
983
984
  void putback_lru_page(struct page *page)
  {
9c4e6b1a7   Shakeel Butt   mm, mlock, vmscan...
985
  	lru_cache_add(page);
894bc3104   Lee Schermerhorn   Unevictable LRU I...
986
987
  	put_page(page);		/* drop ref from isolate */
  }
dfc8d636c   Johannes Weiner   vmscan: factor ou...
988
989
990
  enum page_references {
  	PAGEREF_RECLAIM,
  	PAGEREF_RECLAIM_CLEAN,
645747462   Johannes Weiner   vmscan: detect ma...
991
  	PAGEREF_KEEP,
dfc8d636c   Johannes Weiner   vmscan: factor ou...
992
993
994
995
996
997
  	PAGEREF_ACTIVATE,
  };
  
  static enum page_references page_check_references(struct page *page,
  						  struct scan_control *sc)
  {
645747462   Johannes Weiner   vmscan: detect ma...
998
  	int referenced_ptes, referenced_page;
dfc8d636c   Johannes Weiner   vmscan: factor ou...
999
  	unsigned long vm_flags;
dfc8d636c   Johannes Weiner   vmscan: factor ou...
1000

c3ac9a8ad   Johannes Weiner   mm: memcg: count ...
1001
1002
  	referenced_ptes = page_referenced(page, 1, sc->target_mem_cgroup,
  					  &vm_flags);
645747462   Johannes Weiner   vmscan: detect ma...
1003
  	referenced_page = TestClearPageReferenced(page);
dfc8d636c   Johannes Weiner   vmscan: factor ou...
1004

dfc8d636c   Johannes Weiner   vmscan: factor ou...
1005
1006
1007
1008
1009
1010
  	/*
  	 * Mlock lost the isolation race with us.  Let try_to_unmap()
  	 * move the page to the unevictable list.
  	 */
  	if (vm_flags & VM_LOCKED)
  		return PAGEREF_RECLAIM;
645747462   Johannes Weiner   vmscan: detect ma...
1011
  	if (referenced_ptes) {
e48982734   Michal Hocko   mm: consider all ...
1012
  		if (PageSwapBacked(page))
645747462   Johannes Weiner   vmscan: detect ma...
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
  			return PAGEREF_ACTIVATE;
  		/*
  		 * All mapped pages start out with page table
  		 * references from the instantiating fault, so we need
  		 * to look twice if a mapped file page is used more
  		 * than once.
  		 *
  		 * Mark it and spare it for another trip around the
  		 * inactive list.  Another page table reference will
  		 * lead to its activation.
  		 *
  		 * Note: the mark is set for activated pages as well
  		 * so that recently deactivated but used pages are
  		 * quickly recovered.
  		 */
  		SetPageReferenced(page);
34dbc67a6   Konstantin Khlebnikov   vmscan: promote s...
1029
  		if (referenced_page || referenced_ptes > 1)
645747462   Johannes Weiner   vmscan: detect ma...
1030
  			return PAGEREF_ACTIVATE;
c909e9936   Konstantin Khlebnikov   vmscan: activate ...
1031
1032
1033
1034
1035
  		/*
  		 * Activate file-backed executable pages after first usage.
  		 */
  		if (vm_flags & VM_EXEC)
  			return PAGEREF_ACTIVATE;
645747462   Johannes Weiner   vmscan: detect ma...
1036
1037
  		return PAGEREF_KEEP;
  	}
dfc8d636c   Johannes Weiner   vmscan: factor ou...
1038
1039
  
  	/* Reclaim if clean, defer dirty pages to writeback */
2e30244a7   KOSAKI Motohiro   vmscan,tmpfs: tre...
1040
  	if (referenced_page && !PageSwapBacked(page))
645747462   Johannes Weiner   vmscan: detect ma...
1041
1042
1043
  		return PAGEREF_RECLAIM_CLEAN;
  
  	return PAGEREF_RECLAIM;
dfc8d636c   Johannes Weiner   vmscan: factor ou...
1044
  }
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1045
1046
1047
1048
  /* Check if a page is dirty or under writeback */
  static void page_check_dirty_writeback(struct page *page,
  				       bool *dirty, bool *writeback)
  {
b45972265   Mel Gorman   mm: vmscan: take ...
1049
  	struct address_space *mapping;
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1050
1051
1052
1053
  	/*
  	 * Anonymous pages are not handled by flushers and must be written
  	 * from reclaim context. Do not stall reclaim based on them
  	 */
802a3a92a   Shaohua Li   mm: reclaim MADV_...
1054
1055
  	if (!page_is_file_cache(page) ||
  	    (PageAnon(page) && !PageSwapBacked(page))) {
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1056
1057
1058
1059
1060
1061
1062
1063
  		*dirty = false;
  		*writeback = false;
  		return;
  	}
  
  	/* By default assume that the page flags are accurate */
  	*dirty = PageDirty(page);
  	*writeback = PageWriteback(page);
b45972265   Mel Gorman   mm: vmscan: take ...
1064
1065
1066
1067
1068
1069
1070
1071
  
  	/* Verify dirty/writeback state if the filesystem supports it */
  	if (!page_has_private(page))
  		return;
  
  	mapping = page_mapping(page);
  	if (mapping && mapping->a_ops->is_dirty_writeback)
  		mapping->a_ops->is_dirty_writeback(page, dirty, writeback);
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1072
  }
e286781d5   Nick Piggin   mm: speculative p...
1073
  /*
1742f19fa   Andrew Morton   [PATCH] vmscan: r...
1074
   * shrink_page_list() returns the number of reclaimed pages
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1075
   */
1742f19fa   Andrew Morton   [PATCH] vmscan: r...
1076
  static unsigned long shrink_page_list(struct list_head *page_list,
599d0c954   Mel Gorman   mm, vmscan: move ...
1077
  				      struct pglist_data *pgdat,
f84f6e2b0   Mel Gorman   mm: vmscan: do no...
1078
  				      struct scan_control *sc,
02c6de8d7   Minchan Kim   mm: cma: discard ...
1079
  				      enum ttu_flags ttu_flags,
3c710c1ad   Michal Hocko   mm, vmscan: extra...
1080
  				      struct reclaim_stat *stat,
8940b34a4   Minchan Kim   mm: change PAGERE...
1081
  				      bool ignore_references)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1082
1083
  {
  	LIST_HEAD(ret_pages);
abe4c3b50   Mel Gorman   vmscan: set up pa...
1084
  	LIST_HEAD(free_pages);
3c710c1ad   Michal Hocko   mm, vmscan: extra...
1085
  	unsigned nr_reclaimed = 0;
886cf1901   Kirill Tkhai   mm: move recent_r...
1086
  	unsigned pgactivate = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1087

060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1088
  	memset(stat, 0, sizeof(*stat));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1089
  	cond_resched();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1090
1091
1092
1093
  	while (!list_empty(page_list)) {
  		struct address_space *mapping;
  		struct page *page;
  		int may_enter_fs;
8940b34a4   Minchan Kim   mm: change PAGERE...
1094
  		enum page_references references = PAGEREF_RECLAIM;
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1095
  		bool dirty, writeback;
98879b3b9   Yang Shi   mm: vmscan: corre...
1096
  		unsigned int nr_pages;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1097
1098
1099
1100
1101
  
  		cond_resched();
  
  		page = lru_to_page(page_list);
  		list_del(&page->lru);
529ae9aaa   Nick Piggin   mm: rename page t...
1102
  		if (!trylock_page(page))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1103
  			goto keep;
309381fea   Sasha Levin   mm: dump page whe...
1104
  		VM_BUG_ON_PAGE(PageActive(page), page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1105

d8c6546b1   Matthew Wilcox (Oracle)   mm: introduce com...
1106
  		nr_pages = compound_nr(page);
98879b3b9   Yang Shi   mm: vmscan: corre...
1107
1108
1109
  
  		/* Account the number of base pages even though THP */
  		sc->nr_scanned += nr_pages;
80e434260   Christoph Lameter   [PATCH] zone recl...
1110

39b5f29ac   Hugh Dickins   mm: remove vma ar...
1111
  		if (unlikely(!page_evictable(page)))
ad6b67041   Minchan Kim   mm: remove SWAP_M...
1112
  			goto activate_locked;
894bc3104   Lee Schermerhorn   Unevictable LRU I...
1113

a6dc60f89   Johannes Weiner   vmscan: rename sc...
1114
  		if (!sc->may_unmap && page_mapped(page))
80e434260   Christoph Lameter   [PATCH] zone recl...
1115
  			goto keep_locked;
c661b078f   Andy Whitcroft   synchronous lumpy...
1116
1117
  		may_enter_fs = (sc->gfp_mask & __GFP_FS) ||
  			(PageSwapCache(page) && (sc->gfp_mask & __GFP_IO));
283aba9f9   Mel Gorman   mm: vmscan: block...
1118
  		/*
894befec4   Andrey Ryabinin   mm/vmscan: update...
1119
  		 * The number of dirty pages determines if a node is marked
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1120
1121
1122
1123
1124
1125
  		 * reclaim_congested which affects wait_iff_congested. kswapd
  		 * will stall and start writing pages if the tail of the LRU
  		 * is all dirty unqueued pages.
  		 */
  		page_check_dirty_writeback(page, &dirty, &writeback);
  		if (dirty || writeback)
060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1126
  			stat->nr_dirty++;
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1127
1128
  
  		if (dirty && !writeback)
060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1129
  			stat->nr_unqueued_dirty++;
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1130

d04e8acd0   Mel Gorman   mm: vmscan: treat...
1131
1132
1133
1134
1135
1136
  		/*
  		 * Treat this page as congested if the underlying BDI is or if
  		 * pages are cycling through the LRU so quickly that the
  		 * pages marked for immediate reclaim are making it to the
  		 * end of the LRU a second time.
  		 */
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1137
  		mapping = page_mapping(page);
1da58ee2a   Jamie Liu   mm: vmscan: count...
1138
  		if (((dirty || writeback) && mapping &&
703c27088   Tejun Heo   writeback: implem...
1139
  		     inode_write_congested(mapping->host)) ||
d04e8acd0   Mel Gorman   mm: vmscan: treat...
1140
  		    (writeback && PageReclaim(page)))
060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1141
  			stat->nr_congested++;
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1142
1143
  
  		/*
283aba9f9   Mel Gorman   mm: vmscan: block...
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
  		 * If a page at the tail of the LRU is under writeback, there
  		 * are three cases to consider.
  		 *
  		 * 1) If reclaim is encountering an excessive number of pages
  		 *    under writeback and this page is both under writeback and
  		 *    PageReclaim then it indicates that pages are being queued
  		 *    for IO but are being recycled through the LRU before the
  		 *    IO can complete. Waiting on the page itself risks an
  		 *    indefinite stall if it is impossible to writeback the
  		 *    page due to IO error or disconnected storage so instead
b1a6f21e3   Mel Gorman   mm: vmscan: stall...
1154
1155
  		 *    note that the LRU is being scanned too quickly and the
  		 *    caller can stall after page list has been processed.
283aba9f9   Mel Gorman   mm: vmscan: block...
1156
  		 *
97c9341f7   Tejun Heo   mm: vmscan: disab...
1157
  		 * 2) Global or new memcg reclaim encounters a page that is
ecf5fc6e9   Michal Hocko   mm, vmscan: Do no...
1158
1159
1160
  		 *    not marked for immediate reclaim, or the caller does not
  		 *    have __GFP_FS (or __GFP_IO if it's simply going to swap,
  		 *    not to fs). In this case mark the page for immediate
97c9341f7   Tejun Heo   mm: vmscan: disab...
1161
  		 *    reclaim and continue scanning.
283aba9f9   Mel Gorman   mm: vmscan: block...
1162
  		 *
ecf5fc6e9   Michal Hocko   mm, vmscan: Do no...
1163
1164
  		 *    Require may_enter_fs because we would wait on fs, which
  		 *    may not have submitted IO yet. And the loop driver might
283aba9f9   Mel Gorman   mm: vmscan: block...
1165
1166
1167
1168
1169
  		 *    enter reclaim, and deadlock if it waits on a page for
  		 *    which it is needed to do the write (loop masks off
  		 *    __GFP_IO|__GFP_FS for this reason); but more thought
  		 *    would probably show more reasons.
  		 *
7fadc8202   Hugh Dickins   mm, vmscan: unloc...
1170
  		 * 3) Legacy memcg encounters a page that is already marked
283aba9f9   Mel Gorman   mm: vmscan: block...
1171
1172
1173
1174
  		 *    PageReclaim. memcg does not have any dirty pages
  		 *    throttling so we could easily OOM just because too many
  		 *    pages are in writeback and there is nothing else to
  		 *    reclaim. Wait for the writeback to complete.
c55e8d035   Johannes Weiner   mm: vmscan: move ...
1175
1176
1177
1178
1179
1180
1181
1182
1183
  		 *
  		 * In cases 1) and 2) we activate the pages to get them out of
  		 * the way while we continue scanning for clean pages on the
  		 * inactive list and refilling from the active list. The
  		 * observation here is that waiting for disk writes is more
  		 * expensive than potentially causing reloads down the line.
  		 * Since they're marked for immediate reclaim, they won't put
  		 * memory pressure on the cache working set any longer than it
  		 * takes to write them to disk.
283aba9f9   Mel Gorman   mm: vmscan: block...
1184
  		 */
c661b078f   Andy Whitcroft   synchronous lumpy...
1185
  		if (PageWriteback(page)) {
283aba9f9   Mel Gorman   mm: vmscan: block...
1186
1187
1188
  			/* Case 1 above */
  			if (current_is_kswapd() &&
  			    PageReclaim(page) &&
599d0c954   Mel Gorman   mm, vmscan: move ...
1189
  			    test_bit(PGDAT_WRITEBACK, &pgdat->flags)) {
060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1190
  				stat->nr_immediate++;
c55e8d035   Johannes Weiner   mm: vmscan: move ...
1191
  				goto activate_locked;
283aba9f9   Mel Gorman   mm: vmscan: block...
1192
1193
  
  			/* Case 2 above */
97c9341f7   Tejun Heo   mm: vmscan: disab...
1194
  			} else if (sane_reclaim(sc) ||
ecf5fc6e9   Michal Hocko   mm, vmscan: Do no...
1195
  			    !PageReclaim(page) || !may_enter_fs) {
c3b94f44f   Hugh Dickins   memcg: further pr...
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
  				/*
  				 * This is slightly racy - end_page_writeback()
  				 * might have just cleared PageReclaim, then
  				 * setting PageReclaim here end up interpreted
  				 * as PageReadahead - but that does not matter
  				 * enough to care.  What we do want is for this
  				 * page to have PageReclaim set next time memcg
  				 * reclaim reaches the tests above, so it will
  				 * then wait_on_page_writeback() to avoid OOM;
  				 * and it's also appropriate in global reclaim.
  				 */
  				SetPageReclaim(page);
060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1208
  				stat->nr_writeback++;
c55e8d035   Johannes Weiner   mm: vmscan: move ...
1209
  				goto activate_locked;
283aba9f9   Mel Gorman   mm: vmscan: block...
1210
1211
1212
  
  			/* Case 3 above */
  			} else {
7fadc8202   Hugh Dickins   mm, vmscan: unloc...
1213
  				unlock_page(page);
283aba9f9   Mel Gorman   mm: vmscan: block...
1214
  				wait_on_page_writeback(page);
7fadc8202   Hugh Dickins   mm, vmscan: unloc...
1215
1216
1217
  				/* then go back and try same page again */
  				list_add_tail(&page->lru, page_list);
  				continue;
e62e384e9   Michal Hocko   memcg: prevent OO...
1218
  			}
c661b078f   Andy Whitcroft   synchronous lumpy...
1219
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1220

8940b34a4   Minchan Kim   mm: change PAGERE...
1221
  		if (!ignore_references)
02c6de8d7   Minchan Kim   mm: cma: discard ...
1222
  			references = page_check_references(page, sc);
dfc8d636c   Johannes Weiner   vmscan: factor ou...
1223
1224
  		switch (references) {
  		case PAGEREF_ACTIVATE:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1225
  			goto activate_locked;
645747462   Johannes Weiner   vmscan: detect ma...
1226
  		case PAGEREF_KEEP:
98879b3b9   Yang Shi   mm: vmscan: corre...
1227
  			stat->nr_ref_keep += nr_pages;
645747462   Johannes Weiner   vmscan: detect ma...
1228
  			goto keep_locked;
dfc8d636c   Johannes Weiner   vmscan: factor ou...
1229
1230
1231
1232
  		case PAGEREF_RECLAIM:
  		case PAGEREF_RECLAIM_CLEAN:
  			; /* try to reclaim the page below */
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1233

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1234
1235
1236
  		/*
  		 * Anonymous process memory has backing store?
  		 * Try to allocate it some swap space here.
802a3a92a   Shaohua Li   mm: reclaim MADV_...
1237
  		 * Lazyfree page could be freed directly
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1238
  		 */
bd4c82c22   Huang Ying   mm, THP, swap: de...
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
  		if (PageAnon(page) && PageSwapBacked(page)) {
  			if (!PageSwapCache(page)) {
  				if (!(sc->gfp_mask & __GFP_IO))
  					goto keep_locked;
  				if (PageTransHuge(page)) {
  					/* cannot split THP, skip it */
  					if (!can_split_huge_page(page, NULL))
  						goto activate_locked;
  					/*
  					 * Split pages without a PMD map right
  					 * away. Chances are some or all of the
  					 * tail pages can be freed without IO.
  					 */
  					if (!compound_mapcount(page) &&
  					    split_huge_page_to_list(page,
  								    page_list))
  						goto activate_locked;
  				}
  				if (!add_to_swap(page)) {
  					if (!PageTransHuge(page))
98879b3b9   Yang Shi   mm: vmscan: corre...
1259
  						goto activate_locked_split;
bd4c82c22   Huang Ying   mm, THP, swap: de...
1260
1261
1262
1263
  					/* Fallback to swap normal pages */
  					if (split_huge_page_to_list(page,
  								    page_list))
  						goto activate_locked;
fe490cc0f   Huang Ying   mm, THP, swap: ad...
1264
1265
1266
  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  					count_vm_event(THP_SWPOUT_FALLBACK);
  #endif
bd4c82c22   Huang Ying   mm, THP, swap: de...
1267
  					if (!add_to_swap(page))
98879b3b9   Yang Shi   mm: vmscan: corre...
1268
  						goto activate_locked_split;
bd4c82c22   Huang Ying   mm, THP, swap: de...
1269
  				}
0f0746589   Minchan Kim   mm, THP, swap: mo...
1270

bd4c82c22   Huang Ying   mm, THP, swap: de...
1271
  				may_enter_fs = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1272

bd4c82c22   Huang Ying   mm, THP, swap: de...
1273
1274
1275
  				/* Adding to swap updated mapping */
  				mapping = page_mapping(page);
  			}
7751b2da6   Kirill A. Shutemov   vmscan: split fil...
1276
1277
1278
1279
  		} else if (unlikely(PageTransHuge(page))) {
  			/* Split file THP */
  			if (split_huge_page_to_list(page, page_list))
  				goto keep_locked;
e2be15f6c   Mel Gorman   mm: vmscan: stall...
1280
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1281
1282
  
  		/*
98879b3b9   Yang Shi   mm: vmscan: corre...
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
  		 * THP may get split above, need minus tail pages and update
  		 * nr_pages to avoid accounting tail pages twice.
  		 *
  		 * The tail pages that are added into swap cache successfully
  		 * reach here.
  		 */
  		if ((nr_pages > 1) && !PageTransHuge(page)) {
  			sc->nr_scanned -= (nr_pages - 1);
  			nr_pages = 1;
  		}
  
  		/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1295
1296
1297
  		 * The page is mapped into the page tables of one or more
  		 * processes. Try to unmap it here.
  		 */
802a3a92a   Shaohua Li   mm: reclaim MADV_...
1298
  		if (page_mapped(page)) {
bd4c82c22   Huang Ying   mm, THP, swap: de...
1299
1300
1301
1302
1303
  			enum ttu_flags flags = ttu_flags | TTU_BATCH_FLUSH;
  
  			if (unlikely(PageTransHuge(page)))
  				flags |= TTU_SPLIT_HUGE_PMD;
  			if (!try_to_unmap(page, flags)) {
98879b3b9   Yang Shi   mm: vmscan: corre...
1304
  				stat->nr_unmap_fail += nr_pages;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1305
  				goto activate_locked;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1306
1307
1308
1309
  			}
  		}
  
  		if (PageDirty(page)) {
ee72886d8   Mel Gorman   mm: vmscan: do no...
1310
  			/*
4eda48235   Johannes Weiner   mm: vmscan: only ...
1311
1312
1313
1314
1315
1316
1317
1318
  			 * Only kswapd can writeback filesystem pages
  			 * to avoid risk of stack overflow. But avoid
  			 * injecting inefficient single-page IO into
  			 * flusher writeback as much as possible: only
  			 * write pages when we've encountered many
  			 * dirty pages, and when we've already scanned
  			 * the rest of the LRU for clean pages and see
  			 * the same dirty pages again (PageReclaim).
ee72886d8   Mel Gorman   mm: vmscan: do no...
1319
  			 */
f84f6e2b0   Mel Gorman   mm: vmscan: do no...
1320
  			if (page_is_file_cache(page) &&
4eda48235   Johannes Weiner   mm: vmscan: only ...
1321
1322
  			    (!current_is_kswapd() || !PageReclaim(page) ||
  			     !test_bit(PGDAT_DIRTY, &pgdat->flags))) {
49ea7eb65   Mel Gorman   mm: vmscan: immed...
1323
1324
1325
1326
1327
1328
  				/*
  				 * Immediately reclaim when written back.
  				 * Similar in principal to deactivate_page()
  				 * except we already have the page isolated
  				 * and know it's dirty
  				 */
c4a25635b   Mel Gorman   mm: move vmscan w...
1329
  				inc_node_page_state(page, NR_VMSCAN_IMMEDIATE);
49ea7eb65   Mel Gorman   mm: vmscan: immed...
1330
  				SetPageReclaim(page);
c55e8d035   Johannes Weiner   mm: vmscan: move ...
1331
  				goto activate_locked;
ee72886d8   Mel Gorman   mm: vmscan: do no...
1332
  			}
dfc8d636c   Johannes Weiner   vmscan: factor ou...
1333
  			if (references == PAGEREF_RECLAIM_CLEAN)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1334
  				goto keep_locked;
4dd4b9202   Andrew Morton   revert "kswapd sh...
1335
  			if (!may_enter_fs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1336
  				goto keep_locked;
52a8363ea   Christoph Lameter   [PATCH] mm: impro...
1337
  			if (!sc->may_writepage)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1338
  				goto keep_locked;
d950c9477   Mel Gorman   mm: defer flush o...
1339
1340
1341
1342
1343
1344
  			/*
  			 * Page is dirty. Flush the TLB if a writable entry
  			 * potentially exists to avoid CPU writes after IO
  			 * starts and then write it out here.
  			 */
  			try_to_unmap_flush_dirty();
7d3579e8e   KOSAKI Motohiro   vmscan: narrow th...
1345
  			switch (pageout(page, mapping, sc)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1346
1347
1348
1349
1350
  			case PAGE_KEEP:
  				goto keep_locked;
  			case PAGE_ACTIVATE:
  				goto activate_locked;
  			case PAGE_SUCCESS:
7d3579e8e   KOSAKI Motohiro   vmscan: narrow th...
1351
  				if (PageWriteback(page))
41ac1999c   Mel Gorman   mm: vmscan: do no...
1352
  					goto keep;
7d3579e8e   KOSAKI Motohiro   vmscan: narrow th...
1353
  				if (PageDirty(page))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1354
  					goto keep;
7d3579e8e   KOSAKI Motohiro   vmscan: narrow th...
1355

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1356
1357
1358
1359
  				/*
  				 * A synchronous write - probably a ramdisk.  Go
  				 * ahead and try to reclaim the page.
  				 */
529ae9aaa   Nick Piggin   mm: rename page t...
1360
  				if (!trylock_page(page))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
  					goto keep;
  				if (PageDirty(page) || PageWriteback(page))
  					goto keep_locked;
  				mapping = page_mapping(page);
  			case PAGE_CLEAN:
  				; /* try to free the page below */
  			}
  		}
  
  		/*
  		 * If the page has buffers, try to free the buffer mappings
  		 * associated with this page. If we succeed we try to free
  		 * the page as well.
  		 *
  		 * We do this even if the page is PageDirty().
  		 * try_to_release_page() does not perform I/O, but it is
  		 * possible for a page to have PageDirty set, but it is actually
  		 * clean (all its buffers are clean).  This happens if the
  		 * buffers were written out directly, with submit_bh(). ext3
894bc3104   Lee Schermerhorn   Unevictable LRU I...
1380
  		 * will do this, as well as the blockdev mapping.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
  		 * try_to_release_page() will discover that cleanness and will
  		 * drop the buffers and mark the page clean - it can be freed.
  		 *
  		 * Rarely, pages can have buffers and no ->mapping.  These are
  		 * the pages which were not successfully invalidated in
  		 * truncate_complete_page().  We try to drop those buffers here
  		 * and if that worked, and the page is no longer mapped into
  		 * process address space (page_count == 1) it can be freed.
  		 * Otherwise, leave the page on the LRU so it is swappable.
  		 */
266cf658e   David Howells   FS-Cache: Recruit...
1391
  		if (page_has_private(page)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1392
1393
  			if (!try_to_release_page(page, sc->gfp_mask))
  				goto activate_locked;
e286781d5   Nick Piggin   mm: speculative p...
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
  			if (!mapping && page_count(page) == 1) {
  				unlock_page(page);
  				if (put_page_testzero(page))
  					goto free_it;
  				else {
  					/*
  					 * rare race with speculative reference.
  					 * the speculative reference will free
  					 * this page shortly, so we may
  					 * increment nr_reclaimed here (and
  					 * leave it off the LRU).
  					 */
  					nr_reclaimed++;
  					continue;
  				}
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1410
  		}
802a3a92a   Shaohua Li   mm: reclaim MADV_...
1411
1412
1413
1414
1415
1416
1417
1418
  		if (PageAnon(page) && !PageSwapBacked(page)) {
  			/* follow __remove_mapping for reference */
  			if (!page_ref_freeze(page, 1))
  				goto keep_locked;
  			if (PageDirty(page)) {
  				page_ref_unfreeze(page, 1);
  				goto keep_locked;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1419

802a3a92a   Shaohua Li   mm: reclaim MADV_...
1420
  			count_vm_event(PGLAZYFREED);
2262185c5   Roman Gushchin   mm: per-cgroup me...
1421
  			count_memcg_page_event(page, PGLAZYFREED);
802a3a92a   Shaohua Li   mm: reclaim MADV_...
1422
1423
  		} else if (!mapping || !__remove_mapping(mapping, page, true))
  			goto keep_locked;
9a1ea439b   Hugh Dickins   mm: put_and_wait_...
1424
1425
  
  		unlock_page(page);
e286781d5   Nick Piggin   mm: speculative p...
1426
  free_it:
98879b3b9   Yang Shi   mm: vmscan: corre...
1427
1428
1429
1430
1431
  		/*
  		 * THP may get swapped out in a whole, need account
  		 * all base pages.
  		 */
  		nr_reclaimed += nr_pages;
abe4c3b50   Mel Gorman   vmscan: set up pa...
1432
1433
1434
1435
1436
  
  		/*
  		 * Is there need to periodically free_page_list? It would
  		 * appear not as the counts should be low
  		 */
7ae88534c   Yang Shi   mm: move mem_cgro...
1437
  		if (unlikely(PageTransHuge(page)))
bd4c82c22   Huang Ying   mm, THP, swap: de...
1438
  			(*get_compound_page_dtor(page))(page);
7ae88534c   Yang Shi   mm: move mem_cgro...
1439
  		else
bd4c82c22   Huang Ying   mm, THP, swap: de...
1440
  			list_add(&page->lru, &free_pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1441
  		continue;
98879b3b9   Yang Shi   mm: vmscan: corre...
1442
1443
1444
1445
1446
1447
1448
1449
1450
  activate_locked_split:
  		/*
  		 * The tail pages that are failed to add into swap cache
  		 * reach here.  Fixup nr_scanned and nr_pages.
  		 */
  		if (nr_pages > 1) {
  			sc->nr_scanned -= (nr_pages - 1);
  			nr_pages = 1;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1451
  activate_locked:
68a22394c   Rik van Riel   vmscan: free swap...
1452
  		/* Not a candidate for swapping, so reclaim swap space. */
ad6b67041   Minchan Kim   mm: remove SWAP_M...
1453
1454
  		if (PageSwapCache(page) && (mem_cgroup_swap_full(page) ||
  						PageMlocked(page)))
a2c43eed8   Hugh Dickins   mm: try_to_free_s...
1455
  			try_to_free_swap(page);
309381fea   Sasha Levin   mm: dump page whe...
1456
  		VM_BUG_ON_PAGE(PageActive(page), page);
ad6b67041   Minchan Kim   mm: remove SWAP_M...
1457
  		if (!PageMlocked(page)) {
886cf1901   Kirill Tkhai   mm: move recent_r...
1458
  			int type = page_is_file_cache(page);
ad6b67041   Minchan Kim   mm: remove SWAP_M...
1459
  			SetPageActive(page);
98879b3b9   Yang Shi   mm: vmscan: corre...
1460
  			stat->nr_activate[type] += nr_pages;
2262185c5   Roman Gushchin   mm: per-cgroup me...
1461
  			count_memcg_page_event(page, PGACTIVATE);
ad6b67041   Minchan Kim   mm: remove SWAP_M...
1462
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1463
1464
1465
1466
  keep_locked:
  		unlock_page(page);
  keep:
  		list_add(&page->lru, &ret_pages);
309381fea   Sasha Levin   mm: dump page whe...
1467
  		VM_BUG_ON_PAGE(PageLRU(page) || PageUnevictable(page), page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1468
  	}
abe4c3b50   Mel Gorman   vmscan: set up pa...
1469

98879b3b9   Yang Shi   mm: vmscan: corre...
1470
  	pgactivate = stat->nr_activate[0] + stat->nr_activate[1];
747db954c   Johannes Weiner   mm: memcontrol: u...
1471
  	mem_cgroup_uncharge_list(&free_pages);
72b252aed   Mel Gorman   mm: send one IPI ...
1472
  	try_to_unmap_flush();
2d4894b5d   Mel Gorman   mm: remove cold p...
1473
  	free_unref_page_list(&free_pages);
abe4c3b50   Mel Gorman   vmscan: set up pa...
1474

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1475
  	list_splice(&ret_pages, page_list);
886cf1901   Kirill Tkhai   mm: move recent_r...
1476
  	count_vm_events(PGACTIVATE, pgactivate);
060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1477

05ff51376   Andrew Morton   [PATCH] vmscan re...
1478
  	return nr_reclaimed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1479
  }
02c6de8d7   Minchan Kim   mm: cma: discard ...
1480
1481
1482
1483
1484
1485
1486
1487
  unsigned long reclaim_clean_pages_from_list(struct zone *zone,
  					    struct list_head *page_list)
  {
  	struct scan_control sc = {
  		.gfp_mask = GFP_KERNEL,
  		.priority = DEF_PRIORITY,
  		.may_unmap = 1,
  	};
060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1488
  	struct reclaim_stat dummy_stat;
3c710c1ad   Michal Hocko   mm, vmscan: extra...
1489
  	unsigned long ret;
02c6de8d7   Minchan Kim   mm: cma: discard ...
1490
1491
1492
1493
  	struct page *page, *next;
  	LIST_HEAD(clean_pages);
  
  	list_for_each_entry_safe(page, next, page_list, lru) {
117aad1e9   Rafael Aquini   mm: avoid reinser...
1494
  		if (page_is_file_cache(page) && !PageDirty(page) &&
a58f2cef2   Minchan Kim   mm/vmscan.c: fix ...
1495
  		    !__PageMovable(page) && !PageUnevictable(page)) {
02c6de8d7   Minchan Kim   mm: cma: discard ...
1496
1497
1498
1499
  			ClearPageActive(page);
  			list_move(&page->lru, &clean_pages);
  		}
  	}
599d0c954   Mel Gorman   mm, vmscan: move ...
1500
  	ret = shrink_page_list(&clean_pages, zone->zone_pgdat, &sc,
060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1501
  			TTU_IGNORE_ACCESS, &dummy_stat, true);
02c6de8d7   Minchan Kim   mm: cma: discard ...
1502
  	list_splice(&clean_pages, page_list);
599d0c954   Mel Gorman   mm, vmscan: move ...
1503
  	mod_node_page_state(zone->zone_pgdat, NR_ISOLATED_FILE, -ret);
02c6de8d7   Minchan Kim   mm: cma: discard ...
1504
1505
  	return ret;
  }
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
  /*
   * Attempt to remove the specified page from its LRU.  Only take this page
   * if it is of the appropriate PageActive status.  Pages which are being
   * freed elsewhere are also ignored.
   *
   * page:	page to consider
   * mode:	one of the LRU isolation modes defined above
   *
   * returns 0 on success, -ve errno on failure.
   */
f3fd4a619   Konstantin Khlebnikov   mm: remove lru ty...
1516
  int __isolate_lru_page(struct page *page, isolate_mode_t mode)
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1517
1518
1519
1520
1521
1522
  {
  	int ret = -EINVAL;
  
  	/* Only take pages on the LRU. */
  	if (!PageLRU(page))
  		return ret;
e46a28790   Minchan Kim   CMA: migrate mloc...
1523
1524
  	/* Compaction should not handle unevictable pages but CMA can do so */
  	if (PageUnevictable(page) && !(mode & ISOLATE_UNEVICTABLE))
894bc3104   Lee Schermerhorn   Unevictable LRU I...
1525
  		return ret;
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1526
  	ret = -EBUSY;
08e552c69   KAMEZAWA Hiroyuki   memcg: synchroniz...
1527

c82449352   Mel Gorman   mm: compaction: m...
1528
1529
1530
1531
1532
  	/*
  	 * To minimise LRU disruption, the caller can indicate that it only
  	 * wants to isolate pages it will be able to operate on without
  	 * blocking - clean pages for the most part.
  	 *
c82449352   Mel Gorman   mm: compaction: m...
1533
1534
1535
  	 * ISOLATE_ASYNC_MIGRATE is used to indicate that it only wants to pages
  	 * that it is possible to migrate without blocking
  	 */
1276ad68e   Johannes Weiner   mm: vmscan: scan ...
1536
  	if (mode & ISOLATE_ASYNC_MIGRATE) {
c82449352   Mel Gorman   mm: compaction: m...
1537
1538
1539
1540
1541
1542
  		/* All the caller can do on PageWriteback is block */
  		if (PageWriteback(page))
  			return ret;
  
  		if (PageDirty(page)) {
  			struct address_space *mapping;
69d763fc6   Mel Gorman   mm: pin address_s...
1543
  			bool migrate_dirty;
c82449352   Mel Gorman   mm: compaction: m...
1544

c82449352   Mel Gorman   mm: compaction: m...
1545
1546
1547
  			/*
  			 * Only pages without mappings or that have a
  			 * ->migratepage callback are possible to migrate
69d763fc6   Mel Gorman   mm: pin address_s...
1548
1549
1550
1551
1552
  			 * without blocking. However, we can be racing with
  			 * truncation so it's necessary to lock the page
  			 * to stabilise the mapping as truncation holds
  			 * the page lock until after the page is removed
  			 * from the page cache.
c82449352   Mel Gorman   mm: compaction: m...
1553
  			 */
69d763fc6   Mel Gorman   mm: pin address_s...
1554
1555
  			if (!trylock_page(page))
  				return ret;
c82449352   Mel Gorman   mm: compaction: m...
1556
  			mapping = page_mapping(page);
145e1a71e   Hugh Dickins   mm: fix the NULL ...
1557
  			migrate_dirty = !mapping || mapping->a_ops->migratepage;
69d763fc6   Mel Gorman   mm: pin address_s...
1558
1559
  			unlock_page(page);
  			if (!migrate_dirty)
c82449352   Mel Gorman   mm: compaction: m...
1560
1561
1562
  				return ret;
  		}
  	}
39deaf858   Minchan Kim   mm: compaction: m...
1563

f80c06736   Minchan Kim   mm: zone_reclaim:...
1564
1565
  	if ((mode & ISOLATE_UNMAPPED) && page_mapped(page))
  		return ret;
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
  	if (likely(get_page_unless_zero(page))) {
  		/*
  		 * Be careful not to clear PageLRU until after we're
  		 * sure the page is not being freed elsewhere -- the
  		 * page release code relies on it.
  		 */
  		ClearPageLRU(page);
  		ret = 0;
  	}
  
  	return ret;
  }
7ee36a14f   Mel Gorman   mm, vmscan: Updat...
1578
1579
1580
1581
1582
1583
  
  /*
   * Update LRU sizes after isolating pages. The LRU size updates must
   * be complete before mem_cgroup_update_lru_size due to a santity check.
   */
  static __always_inline void update_lru_sizes(struct lruvec *lruvec,
b4536f0c8   Michal Hocko   mm, memcg: fix th...
1584
  			enum lru_list lru, unsigned long *nr_zone_taken)
7ee36a14f   Mel Gorman   mm, vmscan: Updat...
1585
  {
7ee36a14f   Mel Gorman   mm, vmscan: Updat...
1586
  	int zid;
7ee36a14f   Mel Gorman   mm, vmscan: Updat...
1587
1588
1589
1590
1591
  	for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  		if (!nr_zone_taken[zid])
  			continue;
  
  		__update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
7ee36a14f   Mel Gorman   mm, vmscan: Updat...
1592
  #ifdef CONFIG_MEMCG
b4536f0c8   Michal Hocko   mm, memcg: fix th...
1593
  		mem_cgroup_update_lru_size(lruvec, lru, zid, -nr_zone_taken[zid]);
7ee36a14f   Mel Gorman   mm, vmscan: Updat...
1594
  #endif
b4536f0c8   Michal Hocko   mm, memcg: fix th...
1595
  	}
7ee36a14f   Mel Gorman   mm, vmscan: Updat...
1596
  }
f4b7e272b   Andrey Ryabinin   mm: remove zone_l...
1597
1598
  /**
   * pgdat->lru_lock is heavily contended.  Some of the functions that
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1599
1600
1601
1602
1603
1604
1605
1606
   * shrink the lists perform better by taking out a batch of pages
   * and working on them outside the LRU lock.
   *
   * For pagecache intensive workloads, this function is the hottest
   * spot in the kernel (apart from copy_*_user functions).
   *
   * Appropriate locks must be held before calling this function.
   *
791b48b64   Minchan Kim   mm: vmscan: scan ...
1607
   * @nr_to_scan:	The number of eligible pages to look through on the list.
5dc35979e   Konstantin Khlebnikov   mm/vmscan: push l...
1608
   * @lruvec:	The LRU vector to pull pages from.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1609
   * @dst:	The temp list to put pages on to.
f626012db   Hugh Dickins   mm: remove isolat...
1610
   * @nr_scanned:	The number of pages that were scanned.
fe2c2a106   Rik van Riel   vmscan: reclaim a...
1611
   * @sc:		The scan_control struct for this reclaim session
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1612
   * @mode:	One of the LRU isolation modes
3cb994517   Konstantin Khlebnikov   mm: push lru inde...
1613
   * @lru:	LRU list id for isolating
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1614
1615
1616
   *
   * returns how many pages were moved onto *@dst.
   */
69e05944a   Andrew Morton   [PATCH] vmscan: u...
1617
  static unsigned long isolate_lru_pages(unsigned long nr_to_scan,
5dc35979e   Konstantin Khlebnikov   mm/vmscan: push l...
1618
  		struct lruvec *lruvec, struct list_head *dst,
fe2c2a106   Rik van Riel   vmscan: reclaim a...
1619
  		unsigned long *nr_scanned, struct scan_control *sc,
a9e7c39fa   Kirill Tkhai   mm/vmscan.c: remo...
1620
  		enum lru_list lru)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1621
  {
75b00af77   Hugh Dickins   mm: trivial clean...
1622
  	struct list_head *src = &lruvec->lists[lru];
69e05944a   Andrew Morton   [PATCH] vmscan: u...
1623
  	unsigned long nr_taken = 0;
599d0c954   Mel Gorman   mm, vmscan: move ...
1624
  	unsigned long nr_zone_taken[MAX_NR_ZONES] = { 0 };
7cc30fcfd   Mel Gorman   mm: vmstat: accou...
1625
  	unsigned long nr_skipped[MAX_NR_ZONES] = { 0, };
3db65812d   Johannes Weiner   Revert "mm, vmsca...
1626
  	unsigned long skipped = 0;
791b48b64   Minchan Kim   mm: vmscan: scan ...
1627
  	unsigned long scan, total_scan, nr_pages;
b2e18757f   Mel Gorman   mm, vmscan: begin...
1628
  	LIST_HEAD(pages_skipped);
a9e7c39fa   Kirill Tkhai   mm/vmscan.c: remo...
1629
  	isolate_mode_t mode = (sc->may_unmap ? 0 : ISOLATE_UNMAPPED);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1630

98879b3b9   Yang Shi   mm: vmscan: corre...
1631
  	total_scan = 0;
791b48b64   Minchan Kim   mm: vmscan: scan ...
1632
  	scan = 0;
98879b3b9   Yang Shi   mm: vmscan: corre...
1633
  	while (scan < nr_to_scan && !list_empty(src)) {
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1634
  		struct page *page;
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1635

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1636
1637
  		page = lru_to_page(src);
  		prefetchw_prev_lru_page(page, src, flags);
309381fea   Sasha Levin   mm: dump page whe...
1638
  		VM_BUG_ON_PAGE(!PageLRU(page), page);
8d438f96d   Nick Piggin   [PATCH] mm: PageL...
1639

d8c6546b1   Matthew Wilcox (Oracle)   mm: introduce com...
1640
  		nr_pages = compound_nr(page);
98879b3b9   Yang Shi   mm: vmscan: corre...
1641
  		total_scan += nr_pages;
b2e18757f   Mel Gorman   mm, vmscan: begin...
1642
1643
  		if (page_zonenum(page) > sc->reclaim_idx) {
  			list_move(&page->lru, &pages_skipped);
98879b3b9   Yang Shi   mm: vmscan: corre...
1644
  			nr_skipped[page_zonenum(page)] += nr_pages;
b2e18757f   Mel Gorman   mm, vmscan: begin...
1645
1646
  			continue;
  		}
791b48b64   Minchan Kim   mm: vmscan: scan ...
1647
1648
1649
1650
1651
  		/*
  		 * Do not count skipped pages because that makes the function
  		 * return with no isolated pages if the LRU mostly contains
  		 * ineligible pages.  This causes the VM to not reclaim any
  		 * pages, triggering a premature OOM.
98879b3b9   Yang Shi   mm: vmscan: corre...
1652
1653
1654
1655
  		 *
  		 * Account all tail pages of THP.  This would not cause
  		 * premature OOM since __isolate_lru_page() returns -EBUSY
  		 * only when the page is being freed somewhere else.
791b48b64   Minchan Kim   mm: vmscan: scan ...
1656
  		 */
98879b3b9   Yang Shi   mm: vmscan: corre...
1657
  		scan += nr_pages;
f3fd4a619   Konstantin Khlebnikov   mm: remove lru ty...
1658
  		switch (__isolate_lru_page(page, mode)) {
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1659
  		case 0:
599d0c954   Mel Gorman   mm, vmscan: move ...
1660
1661
  			nr_taken += nr_pages;
  			nr_zone_taken[page_zonenum(page)] += nr_pages;
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1662
  			list_move(&page->lru, dst);
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1663
1664
1665
1666
1667
1668
  			break;
  
  		case -EBUSY:
  			/* else it is being freed elsewhere */
  			list_move(&page->lru, src);
  			continue;
46453a6e1   Nick Piggin   [PATCH] mm: never...
1669

5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1670
1671
1672
  		default:
  			BUG();
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1673
  	}
b2e18757f   Mel Gorman   mm, vmscan: begin...
1674
1675
1676
1677
1678
1679
1680
  	/*
  	 * Splice any skipped pages to the start of the LRU list. Note that
  	 * this disrupts the LRU order when reclaiming for lower zones but
  	 * we cannot splice to the tail. If we did then the SWAP_CLUSTER_MAX
  	 * scanning would soon rescan the same pages to skip and put the
  	 * system at risk of premature OOM.
  	 */
7cc30fcfd   Mel Gorman   mm: vmstat: accou...
1681
1682
  	if (!list_empty(&pages_skipped)) {
  		int zid;
3db65812d   Johannes Weiner   Revert "mm, vmsca...
1683
  		list_splice(&pages_skipped, src);
7cc30fcfd   Mel Gorman   mm: vmstat: accou...
1684
1685
1686
1687
1688
  		for (zid = 0; zid < MAX_NR_ZONES; zid++) {
  			if (!nr_skipped[zid])
  				continue;
  
  			__count_zid_vm_events(PGSCAN_SKIP, zid, nr_skipped[zid]);
1265e3a69   Michal Hocko   mm, vmscan: show ...
1689
  			skipped += nr_skipped[zid];
7cc30fcfd   Mel Gorman   mm: vmstat: accou...
1690
1691
  		}
  	}
791b48b64   Minchan Kim   mm: vmscan: scan ...
1692
  	*nr_scanned = total_scan;
1265e3a69   Michal Hocko   mm, vmscan: show ...
1693
  	trace_mm_vmscan_lru_isolate(sc->reclaim_idx, sc->order, nr_to_scan,
791b48b64   Minchan Kim   mm: vmscan: scan ...
1694
  				    total_scan, skipped, nr_taken, mode, lru);
b4536f0c8   Michal Hocko   mm, memcg: fix th...
1695
  	update_lru_sizes(lruvec, lru, nr_zone_taken);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1696
1697
  	return nr_taken;
  }
62695a84e   Nick Piggin   vmscan: move isol...
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
  /**
   * isolate_lru_page - tries to isolate a page from its LRU list
   * @page: page to isolate from its LRU list
   *
   * Isolates a @page from an LRU list, clears PageLRU and adjusts the
   * vmstat statistic corresponding to whatever LRU list the page was on.
   *
   * Returns 0 if the page was removed from an LRU list.
   * Returns -EBUSY if the page was not on an LRU list.
   *
   * The returned page will have PageLRU() cleared.  If it was found on
894bc3104   Lee Schermerhorn   Unevictable LRU I...
1709
1710
1711
   * the active list, it will have PageActive set.  If it was found on
   * the unevictable list, it will have the PageUnevictable bit set. That flag
   * may need to be cleared by the caller before letting the page go.
62695a84e   Nick Piggin   vmscan: move isol...
1712
1713
1714
1715
1716
   *
   * The vmstat statistic corresponding to the list on which the page was
   * found will be decremented.
   *
   * Restrictions:
a5d09bed7   Mike Rapoport   mm: docs: add bla...
1717
   *
62695a84e   Nick Piggin   vmscan: move isol...
1718
1719
1720
1721
1722
1723
1724
1725
1726
   * (1) Must be called with an elevated refcount on the page. This is a
   *     fundamentnal difference from isolate_lru_pages (which is called
   *     without a stable reference).
   * (2) the lru_lock must not be held.
   * (3) interrupts must be enabled.
   */
  int isolate_lru_page(struct page *page)
  {
  	int ret = -EBUSY;
309381fea   Sasha Levin   mm: dump page whe...
1727
  	VM_BUG_ON_PAGE(!page_count(page), page);
cf2a82ee4   Kirill A. Shutemov   mm: downgrade VM_...
1728
  	WARN_RATELIMIT(PageTail(page), "trying to isolate tail page");
0c917313a   Konstantin Khlebnikov   mm: strictly requ...
1729

62695a84e   Nick Piggin   vmscan: move isol...
1730
  	if (PageLRU(page)) {
f4b7e272b   Andrey Ryabinin   mm: remove zone_l...
1731
  		pg_data_t *pgdat = page_pgdat(page);
fa9add641   Hugh Dickins   mm/memcg: apply a...
1732
  		struct lruvec *lruvec;
62695a84e   Nick Piggin   vmscan: move isol...
1733

f4b7e272b   Andrey Ryabinin   mm: remove zone_l...
1734
1735
  		spin_lock_irq(&pgdat->lru_lock);
  		lruvec = mem_cgroup_page_lruvec(page, pgdat);
0c917313a   Konstantin Khlebnikov   mm: strictly requ...
1736
  		if (PageLRU(page)) {
894bc3104   Lee Schermerhorn   Unevictable LRU I...
1737
  			int lru = page_lru(page);
0c917313a   Konstantin Khlebnikov   mm: strictly requ...
1738
  			get_page(page);
62695a84e   Nick Piggin   vmscan: move isol...
1739
  			ClearPageLRU(page);
fa9add641   Hugh Dickins   mm/memcg: apply a...
1740
1741
  			del_page_from_lru_list(page, lruvec, lru);
  			ret = 0;
62695a84e   Nick Piggin   vmscan: move isol...
1742
  		}
f4b7e272b   Andrey Ryabinin   mm: remove zone_l...
1743
  		spin_unlock_irq(&pgdat->lru_lock);
62695a84e   Nick Piggin   vmscan: move isol...
1744
1745
1746
  	}
  	return ret;
  }
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1747
  /*
d37dd5dcb   Fengguang Wu   vmscan: comment t...
1748
1749
1750
1751
1752
   * A direct reclaimer may isolate SWAP_CLUSTER_MAX pages from the LRU list and
   * then get resheduled. When there are massive number of tasks doing page
   * allocation, such sleeping direct reclaimers may keep piling up on each CPU,
   * the LRU list will go small and be scanned faster than necessary, leading to
   * unnecessary swapping, thrashing and OOM.
35cd78156   Rik van Riel   vmscan: throttle ...
1753
   */
599d0c954   Mel Gorman   mm, vmscan: move ...
1754
  static int too_many_isolated(struct pglist_data *pgdat, int file,
35cd78156   Rik van Riel   vmscan: throttle ...
1755
1756
1757
1758
1759
1760
  		struct scan_control *sc)
  {
  	unsigned long inactive, isolated;
  
  	if (current_is_kswapd())
  		return 0;
97c9341f7   Tejun Heo   mm: vmscan: disab...
1761
  	if (!sane_reclaim(sc))
35cd78156   Rik van Riel   vmscan: throttle ...
1762
1763
1764
  		return 0;
  
  	if (file) {
599d0c954   Mel Gorman   mm, vmscan: move ...
1765
1766
  		inactive = node_page_state(pgdat, NR_INACTIVE_FILE);
  		isolated = node_page_state(pgdat, NR_ISOLATED_FILE);
35cd78156   Rik van Riel   vmscan: throttle ...
1767
  	} else {
599d0c954   Mel Gorman   mm, vmscan: move ...
1768
1769
  		inactive = node_page_state(pgdat, NR_INACTIVE_ANON);
  		isolated = node_page_state(pgdat, NR_ISOLATED_ANON);
35cd78156   Rik van Riel   vmscan: throttle ...
1770
  	}
3cf23841b   Fengguang Wu   mm/vmscan.c: avoi...
1771
1772
1773
1774
1775
  	/*
  	 * GFP_NOIO/GFP_NOFS callers are allowed to isolate more pages, so they
  	 * won't get blocked by normal direct-reclaimers, forming a circular
  	 * deadlock.
  	 */
d0164adc8   Mel Gorman   mm, page_alloc: d...
1776
  	if ((sc->gfp_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
3cf23841b   Fengguang Wu   mm/vmscan.c: avoi...
1777
  		inactive >>= 3;
35cd78156   Rik van Riel   vmscan: throttle ...
1778
1779
  	return isolated > inactive;
  }
a222f3415   Kirill Tkhai   mm: generalize pu...
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
  /*
   * This moves pages from @list to corresponding LRU list.
   *
   * We move them the other way if the page is referenced by one or more
   * processes, from rmap.
   *
   * If the pages are mostly unmapped, the processing is fast and it is
   * appropriate to hold zone_lru_lock across the whole operation.  But if
   * the pages are mapped, the processing is slow (page_referenced()) so we
   * should drop zone_lru_lock around each page.  It's impossible to balance
   * this, so instead we remove the pages from the LRU while processing them.
   * It is safe to rely on PG_active against the non-LRU pages in here because
   * nobody will play with that bit on a non-LRU page.
   *
   * The downside is that we have to touch page->_refcount against each page.
   * But we had to alter page->flags anyway.
   *
   * Returns the number of pages moved to the given lruvec.
   */
  
  static unsigned noinline_for_stack move_pages_to_lru(struct lruvec *lruvec,
  						     struct list_head *list)
666356297   Mel Gorman   vmscan: set up pa...
1802
  {
599d0c954   Mel Gorman   mm, vmscan: move ...
1803
  	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
a222f3415   Kirill Tkhai   mm: generalize pu...
1804
  	int nr_pages, nr_moved = 0;
3f79768f2   Hugh Dickins   mm: rearrange put...
1805
  	LIST_HEAD(pages_to_free);
a222f3415   Kirill Tkhai   mm: generalize pu...
1806
1807
  	struct page *page;
  	enum lru_list lru;
666356297   Mel Gorman   vmscan: set up pa...
1808

a222f3415   Kirill Tkhai   mm: generalize pu...
1809
1810
  	while (!list_empty(list)) {
  		page = lru_to_page(list);
309381fea   Sasha Levin   mm: dump page whe...
1811
  		VM_BUG_ON_PAGE(PageLRU(page), page);
39b5f29ac   Hugh Dickins   mm: remove vma ar...
1812
  		if (unlikely(!page_evictable(page))) {
a222f3415   Kirill Tkhai   mm: generalize pu...
1813
  			list_del(&page->lru);
599d0c954   Mel Gorman   mm, vmscan: move ...
1814
  			spin_unlock_irq(&pgdat->lru_lock);
666356297   Mel Gorman   vmscan: set up pa...
1815
  			putback_lru_page(page);
599d0c954   Mel Gorman   mm, vmscan: move ...
1816
  			spin_lock_irq(&pgdat->lru_lock);
666356297   Mel Gorman   vmscan: set up pa...
1817
1818
  			continue;
  		}
599d0c954   Mel Gorman   mm, vmscan: move ...
1819
  		lruvec = mem_cgroup_page_lruvec(page, pgdat);
fa9add641   Hugh Dickins   mm/memcg: apply a...
1820

7a608572a   Linus Torvalds   Revert "mm: batch...
1821
  		SetPageLRU(page);
666356297   Mel Gorman   vmscan: set up pa...
1822
  		lru = page_lru(page);
a222f3415   Kirill Tkhai   mm: generalize pu...
1823
1824
1825
1826
  
  		nr_pages = hpage_nr_pages(page);
  		update_lru_size(lruvec, lru, page_zonenum(page), nr_pages);
  		list_move(&page->lru, &lruvec->lists[lru]);
fa9add641   Hugh Dickins   mm/memcg: apply a...
1827

2bcf88796   Hugh Dickins   mm: take pagevecs...
1828
1829
1830
  		if (put_page_testzero(page)) {
  			__ClearPageLRU(page);
  			__ClearPageActive(page);
fa9add641   Hugh Dickins   mm/memcg: apply a...
1831
  			del_page_from_lru_list(page, lruvec, lru);
2bcf88796   Hugh Dickins   mm: take pagevecs...
1832
1833
  
  			if (unlikely(PageCompound(page))) {
599d0c954   Mel Gorman   mm, vmscan: move ...
1834
  				spin_unlock_irq(&pgdat->lru_lock);
2bcf88796   Hugh Dickins   mm: take pagevecs...
1835
  				(*get_compound_page_dtor(page))(page);
599d0c954   Mel Gorman   mm, vmscan: move ...
1836
  				spin_lock_irq(&pgdat->lru_lock);
2bcf88796   Hugh Dickins   mm: take pagevecs...
1837
1838
  			} else
  				list_add(&page->lru, &pages_to_free);
a222f3415   Kirill Tkhai   mm: generalize pu...
1839
1840
  		} else {
  			nr_moved += nr_pages;
666356297   Mel Gorman   vmscan: set up pa...
1841
1842
  		}
  	}
666356297   Mel Gorman   vmscan: set up pa...
1843

3f79768f2   Hugh Dickins   mm: rearrange put...
1844
1845
1846
  	/*
  	 * To save our caller's stack, now use input list for pages to free.
  	 */
a222f3415   Kirill Tkhai   mm: generalize pu...
1847
1848
1849
  	list_splice(&pages_to_free, list);
  
  	return nr_moved;
666356297   Mel Gorman   vmscan: set up pa...
1850
1851
1852
  }
  
  /*
399ba0b95   NeilBrown   mm/vmscan.c: avoi...
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
   * If a kernel thread (such as nfsd for loop-back mounts) services
   * a backing device by writing to the page cache it sets PF_LESS_THROTTLE.
   * In that case we should only throttle if the backing device it is
   * writing to is congested.  In other cases it is safe to throttle.
   */
  static int current_may_throttle(void)
  {
  	return !(current->flags & PF_LESS_THROTTLE) ||
  		current->backing_dev_info == NULL ||
  		bdi_write_congested(current->backing_dev_info);
  }
  
  /*
b2e18757f   Mel Gorman   mm, vmscan: begin...
1866
   * shrink_inactive_list() is a helper for shrink_node().  It returns the number
1742f19fa   Andrew Morton   [PATCH] vmscan: r...
1867
   * of reclaimed pages
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1868
   */
666356297   Mel Gorman   vmscan: set up pa...
1869
  static noinline_for_stack unsigned long
1a93be0e7   Konstantin Khlebnikov   mm/vmscan: push l...
1870
  shrink_inactive_list(unsigned long nr_to_scan, struct lruvec *lruvec,
9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
1871
  		     struct scan_control *sc, enum lru_list lru)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1872
1873
  {
  	LIST_HEAD(page_list);
e247dbce5   KOSAKI Motohiro   vmscan: simplify ...
1874
  	unsigned long nr_scanned;
05ff51376   Andrew Morton   [PATCH] vmscan re...
1875
  	unsigned long nr_reclaimed = 0;
e247dbce5   KOSAKI Motohiro   vmscan: simplify ...
1876
  	unsigned long nr_taken;
060f005f0   Kirill Tkhai   mm/vmscan.c: do n...
1877
  	struct reclaim_stat stat;
3cb994517   Konstantin Khlebnikov   mm: push lru inde...
1878
  	int file = is_file_lru(lru);
f46b79120   Kirill Tkhai   mm/vmscan.c: simp...
1879
  	enum vm_event_item item;
599d0c954   Mel Gorman   mm, vmscan: move ...
1880
  	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
1a93be0e7   Konstantin Khlebnikov   mm/vmscan: push l...
1881
  	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
db73ee0d4   Michal Hocko   mm, vmscan: do no...
1882
  	bool stalled = false;
78dc583d3   KOSAKI Motohiro   vmscan: low order...
1883

599d0c954   Mel Gorman   mm, vmscan: move ...
1884
  	while (unlikely(too_many_isolated(pgdat, file, sc))) {
db73ee0d4   Michal Hocko   mm, vmscan: do no...
1885
1886
1887
1888
1889
1890
  		if (stalled)
  			return 0;
  
  		/* wait a bit for the reclaimer. */
  		msleep(100);
  		stalled = true;
35cd78156   Rik van Riel   vmscan: throttle ...
1891
1892
1893
1894
1895
  
  		/* We are about to die and free our memory. Return now. */
  		if (fatal_signal_pending(current))
  			return SWAP_CLUSTER_MAX;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1896
  	lru_add_drain();
f80c06736   Minchan Kim   mm: zone_reclaim:...
1897

599d0c954   Mel Gorman   mm, vmscan: move ...
1898
  	spin_lock_irq(&pgdat->lru_lock);
b35ea17b7   KOSAKI Motohiro   mm: shrink_inacti...
1899

5dc35979e   Konstantin Khlebnikov   mm/vmscan: push l...
1900
  	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &page_list,
a9e7c39fa   Kirill Tkhai   mm/vmscan.c: remo...
1901
  				     &nr_scanned, sc, lru);
95d918fc0   Konstantin Khlebnikov   mm/vmscan: remove...
1902

599d0c954   Mel Gorman   mm, vmscan: move ...
1903
  	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
9d5e6a9f2   Hugh Dickins   mm: update_lru_si...
1904
  	reclaim_stat->recent_scanned[file] += nr_taken;
95d918fc0   Konstantin Khlebnikov   mm/vmscan: remove...
1905

f46b79120   Kirill Tkhai   mm/vmscan.c: simp...
1906
1907
1908
1909
  	item = current_is_kswapd() ? PGSCAN_KSWAPD : PGSCAN_DIRECT;
  	if (global_reclaim(sc))
  		__count_vm_events(item, nr_scanned);
  	__count_memcg_events(lruvec_memcg(lruvec), item, nr_scanned);
599d0c954   Mel Gorman   mm, vmscan: move ...
1910
  	spin_unlock_irq(&pgdat->lru_lock);
b35ea17b7   KOSAKI Motohiro   mm: shrink_inacti...
1911

d563c0501   Hillf Danton   vmscan: handle is...
1912
  	if (nr_taken == 0)
666356297   Mel Gorman   vmscan: set up pa...
1913
  		return 0;
5ad333eb6   Andy Whitcroft   Lumpy Reclaim V4
1914

a128ca71f   Shaohua Li   mm: delete unnece...
1915
  	nr_reclaimed = shrink_page_list(&page_list, pgdat, sc, 0,
3c710c1ad   Michal Hocko   mm, vmscan: extra...
1916
  				&stat, false);
c661b078f   Andy Whitcroft   synchronous lumpy...
1917

599d0c954   Mel Gorman   mm, vmscan: move ...
1918
  	spin_lock_irq(&pgdat->lru_lock);
3f79768f2   Hugh Dickins   mm: rearrange put...
1919

f46b79120   Kirill Tkhai   mm/vmscan.c: simp...
1920
1921
1922
1923
  	item = current_is_kswapd() ? PGSTEAL_KSWAPD : PGSTEAL_DIRECT;
  	if (global_reclaim(sc))
  		__count_vm_events(item, nr_reclaimed);
  	__count_memcg_events(lruvec_memcg(lruvec), item, nr_reclaimed);
b17f18aff   Kirill Tkhai   mm/vmscan.c: fix ...
1924
1925
  	reclaim_stat->recent_rotated[0] += stat.nr_activate[0];
  	reclaim_stat->recent_rotated[1] += stat.nr_activate[1];
a74609faf   Nick Piggin   [PATCH] mm: page_...
1926

a222f3415   Kirill Tkhai   mm: generalize pu...
1927
  	move_pages_to_lru(lruvec, &page_list);
3f79768f2   Hugh Dickins   mm: rearrange put...
1928

599d0c954   Mel Gorman   mm, vmscan: move ...
1929
  	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
3f79768f2   Hugh Dickins   mm: rearrange put...
1930

599d0c954   Mel Gorman   mm, vmscan: move ...
1931
  	spin_unlock_irq(&pgdat->lru_lock);
3f79768f2   Hugh Dickins   mm: rearrange put...
1932

747db954c   Johannes Weiner   mm: memcontrol: u...
1933
  	mem_cgroup_uncharge_list(&page_list);
2d4894b5d   Mel Gorman   mm: remove cold p...
1934
  	free_unref_page_list(&page_list);
e11da5b4f   Mel Gorman   tracing, vmscan: ...
1935

92df3a723   Mel Gorman   mm: vmscan: throt...
1936
  	/*
1c610d5f9   Andrey Ryabinin   mm/vmscan: wake u...
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
  	 * If dirty pages are scanned that are not queued for IO, it
  	 * implies that flushers are not doing their job. This can
  	 * happen when memory pressure pushes dirty pages to the end of
  	 * the LRU before the dirty limits are breached and the dirty
  	 * data has expired. It can also happen when the proportion of
  	 * dirty pages grows not through writes but through memory
  	 * pressure reclaiming all the clean cache. And in some cases,
  	 * the flushers simply cannot keep up with the allocation
  	 * rate. Nudge the flusher threads in case they are asleep.
  	 */
  	if (stat.nr_unqueued_dirty == nr_taken)
  		wakeup_flusher_threads(WB_REASON_VMSCAN);
d108c7721   Andrey Ryabinin   mm/vmscan: don't ...
1949
1950
1951
1952
1953
1954
1955
1956
  	sc->nr.dirty += stat.nr_dirty;
  	sc->nr.congested += stat.nr_congested;
  	sc->nr.unqueued_dirty += stat.nr_unqueued_dirty;
  	sc->nr.writeback += stat.nr_writeback;
  	sc->nr.immediate += stat.nr_immediate;
  	sc->nr.taken += nr_taken;
  	if (file)
  		sc->nr.file_taken += nr_taken;
8e9502828   Mel Gorman   mm: vmscan: move ...
1957

599d0c954   Mel Gorman   mm, vmscan: move ...
1958
  	trace_mm_vmscan_lru_shrink_inactive(pgdat->node_id,
d51d1e645   Steven Rostedt   mm, vmscan, traci...
1959
  			nr_scanned, nr_reclaimed, &stat, sc->priority, file);
05ff51376   Andrew Morton   [PATCH] vmscan re...
1960
  	return nr_reclaimed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1961
  }
f626012db   Hugh Dickins   mm: remove isolat...
1962
  static void shrink_active_list(unsigned long nr_to_scan,
1a93be0e7   Konstantin Khlebnikov   mm/vmscan: push l...
1963
  			       struct lruvec *lruvec,
f16015fbf   Johannes Weiner   mm: vmscan: disti...
1964
  			       struct scan_control *sc,
9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
1965
  			       enum lru_list lru)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1966
  {
44c241f16   KOSAKI Motohiro   mm: rename pgmove...
1967
  	unsigned long nr_taken;
f626012db   Hugh Dickins   mm: remove isolat...
1968
  	unsigned long nr_scanned;
6fe6b7e35   Wu Fengguang   vmscan: report vm...
1969
  	unsigned long vm_flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1970
  	LIST_HEAD(l_hold);	/* The pages which were snipped off */
8cab4754d   Wu Fengguang   vmscan: make mapp...
1971
  	LIST_HEAD(l_active);
b69408e88   Christoph Lameter   vmscan: Use an in...
1972
  	LIST_HEAD(l_inactive);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1973
  	struct page *page;
1a93be0e7   Konstantin Khlebnikov   mm/vmscan: push l...
1974
  	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
9d998b4f1   Michal Hocko   mm, vmscan: add a...
1975
1976
  	unsigned nr_deactivate, nr_activate;
  	unsigned nr_rotated = 0;
3cb994517   Konstantin Khlebnikov   mm: push lru inde...
1977
  	int file = is_file_lru(lru);
599d0c954   Mel Gorman   mm, vmscan: move ...
1978
  	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1979
1980
  
  	lru_add_drain();
f80c06736   Minchan Kim   mm: zone_reclaim:...
1981

599d0c954   Mel Gorman   mm, vmscan: move ...
1982
  	spin_lock_irq(&pgdat->lru_lock);
925b7673c   Johannes Weiner   mm: make per-memc...
1983

5dc35979e   Konstantin Khlebnikov   mm/vmscan: push l...
1984
  	nr_taken = isolate_lru_pages(nr_to_scan, lruvec, &l_hold,
a9e7c39fa   Kirill Tkhai   mm/vmscan.c: remo...
1985
  				     &nr_scanned, sc, lru);
89b5fae53   Johannes Weiner   mm: vmscan: disti...
1986

599d0c954   Mel Gorman   mm, vmscan: move ...
1987
  	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, nr_taken);
b7c46d151   Johannes Weiner   mm: drop unneeded...
1988
  	reclaim_stat->recent_scanned[file] += nr_taken;
1cfb419b3   KAMEZAWA Hiroyuki   per-zone and recl...
1989

599d0c954   Mel Gorman   mm, vmscan: move ...
1990
  	__count_vm_events(PGREFILL, nr_scanned);
2fa2690ca   Yafang Shao   mm/vmscan.c: don'...
1991
  	__count_memcg_events(lruvec_memcg(lruvec), PGREFILL, nr_scanned);
9d5e6a9f2   Hugh Dickins   mm: update_lru_si...
1992

599d0c954   Mel Gorman   mm, vmscan: move ...
1993
  	spin_unlock_irq(&pgdat->lru_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1994

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1995
1996
1997
1998
  	while (!list_empty(&l_hold)) {
  		cond_resched();
  		page = lru_to_page(&l_hold);
  		list_del(&page->lru);
7e9cd4842   Rik van Riel   vmscan: fix pagec...
1999

39b5f29ac   Hugh Dickins   mm: remove vma ar...
2000
  		if (unlikely(!page_evictable(page))) {
894bc3104   Lee Schermerhorn   Unevictable LRU I...
2001
2002
2003
  			putback_lru_page(page);
  			continue;
  		}
cc715d99e   Mel Gorman   mm: vmscan: forci...
2004
2005
2006
2007
2008
2009
2010
  		if (unlikely(buffer_heads_over_limit)) {
  			if (page_has_private(page) && trylock_page(page)) {
  				if (page_has_private(page))
  					try_to_release_page(page, 0);
  				unlock_page(page);
  			}
  		}
c3ac9a8ad   Johannes Weiner   mm: memcg: count ...
2011
2012
  		if (page_referenced(page, 0, sc->target_mem_cgroup,
  				    &vm_flags)) {
9992af102   Rik van Riel   thp: scale nr_rot...
2013
  			nr_rotated += hpage_nr_pages(page);
8cab4754d   Wu Fengguang   vmscan: make mapp...
2014
2015
2016
2017
2018
2019
2020
2021
2022
  			/*
  			 * Identify referenced, file-backed active pages and
  			 * give them one more trip around the active list. So
  			 * that executable code get better chances to stay in
  			 * memory under moderate memory pressure.  Anon pages
  			 * are not likely to be evicted by use-once streaming
  			 * IO, plus JVM can create lots of anon VM_EXEC pages,
  			 * so we ignore them here.
  			 */
41e20983f   Wu Fengguang   vmscan: limit VM_...
2023
  			if ((vm_flags & VM_EXEC) && page_is_file_cache(page)) {
8cab4754d   Wu Fengguang   vmscan: make mapp...
2024
2025
2026
2027
  				list_add(&page->lru, &l_active);
  				continue;
  			}
  		}
7e9cd4842   Rik van Riel   vmscan: fix pagec...
2028

5205e56ee   KOSAKI Motohiro   vmscan: move Clea...
2029
  		ClearPageActive(page);	/* we are de-activating */
1899ad18c   Johannes Weiner   mm: workingset: t...
2030
  		SetPageWorkingset(page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2031
2032
  		list_add(&page->lru, &l_inactive);
  	}
b555749aa   Andrew Morton   vmscan: shrink_ac...
2033
  	/*
8cab4754d   Wu Fengguang   vmscan: make mapp...
2034
  	 * Move pages back to the lru list.
b555749aa   Andrew Morton   vmscan: shrink_ac...
2035
  	 */
599d0c954   Mel Gorman   mm, vmscan: move ...
2036
  	spin_lock_irq(&pgdat->lru_lock);
4f98a2fee   Rik van Riel   vmscan: split LRU...
2037
  	/*
8cab4754d   Wu Fengguang   vmscan: make mapp...
2038
2039
2040
  	 * Count referenced pages from currently used mappings as rotated,
  	 * even though only some of them are actually re-activated.  This
  	 * helps balance scan pressure between file and anonymous pages in
7c0db9e91   Jerome Marchand   mm, vmscan: fix a...
2041
  	 * get_scan_count.
7e9cd4842   Rik van Riel   vmscan: fix pagec...
2042
  	 */
b7c46d151   Johannes Weiner   mm: drop unneeded...
2043
  	reclaim_stat->recent_rotated[file] += nr_rotated;
556adecba   Rik van Riel   vmscan: second ch...
2044

a222f3415   Kirill Tkhai   mm: generalize pu...
2045
2046
  	nr_activate = move_pages_to_lru(lruvec, &l_active);
  	nr_deactivate = move_pages_to_lru(lruvec, &l_inactive);
f372d89e5   Kirill Tkhai   mm: remove pages_...
2047
2048
  	/* Keep all free pages in l_active list */
  	list_splice(&l_inactive, &l_active);
9851ac135   Kirill Tkhai   mm: move nr_deact...
2049
2050
2051
  
  	__count_vm_events(PGDEACTIVATE, nr_deactivate);
  	__count_memcg_events(lruvec_memcg(lruvec), PGDEACTIVATE, nr_deactivate);
599d0c954   Mel Gorman   mm, vmscan: move ...
2052
2053
  	__mod_node_page_state(pgdat, NR_ISOLATED_ANON + file, -nr_taken);
  	spin_unlock_irq(&pgdat->lru_lock);
2bcf88796   Hugh Dickins   mm: take pagevecs...
2054

f372d89e5   Kirill Tkhai   mm: remove pages_...
2055
2056
  	mem_cgroup_uncharge_list(&l_active);
  	free_unref_page_list(&l_active);
9d998b4f1   Michal Hocko   mm, vmscan: add a...
2057
2058
  	trace_mm_vmscan_lru_shrink_active(pgdat->node_id, nr_taken, nr_activate,
  			nr_deactivate, nr_rotated, sc->priority, file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2059
  }
1a4e58cce   Minchan Kim   mm: introduce MAD...
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
  unsigned long reclaim_pages(struct list_head *page_list)
  {
  	int nid = -1;
  	unsigned long nr_reclaimed = 0;
  	LIST_HEAD(node_page_list);
  	struct reclaim_stat dummy_stat;
  	struct page *page;
  	struct scan_control sc = {
  		.gfp_mask = GFP_KERNEL,
  		.priority = DEF_PRIORITY,
  		.may_writepage = 1,
  		.may_unmap = 1,
  		.may_swap = 1,
  	};
  
  	while (!list_empty(page_list)) {
  		page = lru_to_page(page_list);
  		if (nid == -1) {
  			nid = page_to_nid(page);
  			INIT_LIST_HEAD(&node_page_list);
  		}
  
  		if (nid == page_to_nid(page)) {
  			ClearPageActive(page);
  			list_move(&page->lru, &node_page_list);
  			continue;
  		}
  
  		nr_reclaimed += shrink_page_list(&node_page_list,
  						NODE_DATA(nid),
  						&sc, 0,
  						&dummy_stat, false);
  		while (!list_empty(&node_page_list)) {
  			page = lru_to_page(&node_page_list);
  			list_del(&page->lru);
  			putback_lru_page(page);
  		}
  
  		nid = -1;
  	}
  
  	if (!list_empty(&node_page_list)) {
  		nr_reclaimed += shrink_page_list(&node_page_list,
  						NODE_DATA(nid),
  						&sc, 0,
  						&dummy_stat, false);
  		while (!list_empty(&node_page_list)) {
  			page = lru_to_page(&node_page_list);
  			list_del(&page->lru);
  			putback_lru_page(page);
  		}
  	}
  
  	return nr_reclaimed;
  }
59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2115
2116
2117
  /*
   * The inactive anon list should be small enough that the VM never has
   * to do too much work.
14797e236   KOSAKI Motohiro   memcg: add inacti...
2118
   *
59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2119
2120
2121
   * The inactive file list should be small enough to leave most memory
   * to the established workingset on the scan-resistant active list,
   * but large enough to avoid thrashing the aggregate readahead window.
56e49d218   Rik van Riel   vmscan: evict use...
2122
   *
59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2123
2124
   * Both inactive lists should also be large enough that each inactive
   * page has a chance to be referenced again before it is reclaimed.
56e49d218   Rik van Riel   vmscan: evict use...
2125
   *
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2126
2127
   * If that fails and refaulting is observed, the inactive list grows.
   *
59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2128
   * The inactive_ratio is the target ratio of ACTIVE to INACTIVE pages
3a50d14d0   Andrey Ryabinin   mm: remove unused...
2129
   * on this LRU, maintained by the pageout code. An inactive_ratio
59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2130
   * of 3 means 3:1 or 25% of the pages are kept on the inactive list.
56e49d218   Rik van Riel   vmscan: evict use...
2131
   *
59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
   * total     target    max
   * memory    ratio     inactive
   * -------------------------------------
   *   10MB       1         5MB
   *  100MB       1        50MB
   *    1GB       3       250MB
   *   10GB      10       0.9GB
   *  100GB      31         3GB
   *    1TB     101        10GB
   *   10TB     320        32GB
56e49d218   Rik van Riel   vmscan: evict use...
2142
   */
f8d1a3116   Mel Gorman   mm: consider whet...
2143
  static bool inactive_list_is_low(struct lruvec *lruvec, bool file,
2c012a4ad   Kuo-Hsin Yang   mm: vmscan: scan ...
2144
  				 struct scan_control *sc, bool trace)
56e49d218   Rik van Riel   vmscan: evict use...
2145
  {
fd5388037   Michal Hocko   mm, vmscan: clean...
2146
  	enum lru_list active_lru = file * LRU_FILE + LRU_ACTIVE;
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2147
2148
2149
2150
2151
  	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
  	enum lru_list inactive_lru = file * LRU_FILE;
  	unsigned long inactive, active;
  	unsigned long inactive_ratio;
  	unsigned long refaults;
59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2152
  	unsigned long gb;
e3790144c   Johannes Weiner   mm: refactor inac...
2153

59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2154
2155
2156
2157
2158
2159
  	/*
  	 * If we don't have swap space, anonymous page deactivation
  	 * is pointless.
  	 */
  	if (!file && !total_swap_pages)
  		return false;
56e49d218   Rik van Riel   vmscan: evict use...
2160

fd5388037   Michal Hocko   mm, vmscan: clean...
2161
2162
  	inactive = lruvec_lru_size(lruvec, inactive_lru, sc->reclaim_idx);
  	active = lruvec_lru_size(lruvec, active_lru, sc->reclaim_idx);
f8d1a3116   Mel Gorman   mm: consider whet...
2163

2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2164
2165
2166
2167
2168
  	/*
  	 * When refaults are being observed, it means a new workingset
  	 * is being established. Disable active list protection to get
  	 * rid of the stale workingset quickly.
  	 */
205b20cc5   Johannes Weiner   mm: memcontrol: m...
2169
  	refaults = lruvec_page_state_local(lruvec, WORKINGSET_ACTIVATE);
2c012a4ad   Kuo-Hsin Yang   mm: vmscan: scan ...
2170
  	if (file && lruvec->refaults != refaults) {
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2171
2172
2173
2174
2175
2176
2177
2178
  		inactive_ratio = 0;
  	} else {
  		gb = (inactive + active) >> (30 - PAGE_SHIFT);
  		if (gb)
  			inactive_ratio = int_sqrt(10 * gb);
  		else
  			inactive_ratio = 1;
  	}
59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2179

2c012a4ad   Kuo-Hsin Yang   mm: vmscan: scan ...
2180
  	if (trace)
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2181
2182
2183
2184
  		trace_mm_vmscan_inactive_list_is_low(pgdat->node_id, sc->reclaim_idx,
  			lruvec_lru_size(lruvec, inactive_lru, MAX_NR_ZONES), inactive,
  			lruvec_lru_size(lruvec, active_lru, MAX_NR_ZONES), active,
  			inactive_ratio, file);
fd5388037   Michal Hocko   mm, vmscan: clean...
2185

59dc76b0d   Rik van Riel   mm: vmscan: reduc...
2186
  	return inactive * inactive_ratio < active;
b39415b27   Rik van Riel   vmscan: do not ev...
2187
  }
4f98a2fee   Rik van Riel   vmscan: split LRU...
2188
  static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan,
3b991208b   Johannes Weiner   mm: fix inactive ...
2189
  				 struct lruvec *lruvec, struct scan_control *sc)
b69408e88   Christoph Lameter   vmscan: Use an in...
2190
  {
b39415b27   Rik van Riel   vmscan: do not ev...
2191
  	if (is_active_lru(lru)) {
3b991208b   Johannes Weiner   mm: fix inactive ...
2192
  		if (inactive_list_is_low(lruvec, is_file_lru(lru), sc, true))
1a93be0e7   Konstantin Khlebnikov   mm/vmscan: push l...
2193
  			shrink_active_list(nr_to_scan, lruvec, sc, lru);
556adecba   Rik van Riel   vmscan: second ch...
2194
2195
  		return 0;
  	}
1a93be0e7   Konstantin Khlebnikov   mm/vmscan: push l...
2196
  	return shrink_inactive_list(nr_to_scan, lruvec, sc, lru);
4f98a2fee   Rik van Riel   vmscan: split LRU...
2197
  }
9a2651140   Johannes Weiner   mm: vmscan: clean...
2198
2199
2200
2201
2202
2203
  enum scan_balance {
  	SCAN_EQUAL,
  	SCAN_FRACT,
  	SCAN_ANON,
  	SCAN_FILE,
  };
4f98a2fee   Rik van Riel   vmscan: split LRU...
2204
2205
2206
2207
2208
2209
  /*
   * Determine how aggressively the anon and file LRU lists should be
   * scanned.  The relative value of each set of LRU lists is determined
   * by looking at the fraction of the pages scanned we did rotate back
   * onto the active list instead of evict.
   *
be7bd59db   Wanpeng Li   mm: fix page recl...
2210
2211
   * nr[0] = anon inactive pages to scan; nr[1] = anon active pages to scan
   * nr[2] = file inactive pages to scan; nr[3] = file active pages to scan
4f98a2fee   Rik van Riel   vmscan: split LRU...
2212
   */
333776785   Vladimir Davydov   mm: vmscan: pass ...
2213
  static void get_scan_count(struct lruvec *lruvec, struct mem_cgroup *memcg,
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
2214
2215
  			   struct scan_control *sc, unsigned long *nr,
  			   unsigned long *lru_pages)
4f98a2fee   Rik van Riel   vmscan: split LRU...
2216
  {
333776785   Vladimir Davydov   mm: vmscan: pass ...
2217
  	int swappiness = mem_cgroup_swappiness(memcg);
9a2651140   Johannes Weiner   mm: vmscan: clean...
2218
2219
2220
  	struct zone_reclaim_stat *reclaim_stat = &lruvec->reclaim_stat;
  	u64 fraction[2];
  	u64 denominator = 0;	/* gcc */
599d0c954   Mel Gorman   mm, vmscan: move ...
2221
  	struct pglist_data *pgdat = lruvec_pgdat(lruvec);
4f98a2fee   Rik van Riel   vmscan: split LRU...
2222
  	unsigned long anon_prio, file_prio;
9a2651140   Johannes Weiner   mm: vmscan: clean...
2223
  	enum scan_balance scan_balance;
0bf1457f0   Johannes Weiner   mm: vmscan: do no...
2224
  	unsigned long anon, file;
4f98a2fee   Rik van Riel   vmscan: split LRU...
2225
  	unsigned long ap, fp;
4111304da   Hugh Dickins   mm: enum lru_list...
2226
  	enum lru_list lru;
76a33fc38   Shaohua Li   vmscan: prevent g...
2227
2228
  
  	/* If we have no swap space, do not bother scanning anon pages. */
d8b38438a   Vladimir Davydov   mm: vmscan: do no...
2229
  	if (!sc->may_swap || mem_cgroup_get_nr_swap_pages(memcg) <= 0) {
9a2651140   Johannes Weiner   mm: vmscan: clean...
2230
  		scan_balance = SCAN_FILE;
76a33fc38   Shaohua Li   vmscan: prevent g...
2231
2232
  		goto out;
  	}
4f98a2fee   Rik van Riel   vmscan: split LRU...
2233

10316b313   Johannes Weiner   mm: vmscan: clari...
2234
2235
2236
2237
2238
2239
2240
  	/*
  	 * Global reclaim will swap to prevent OOM even with no
  	 * swappiness, but memcg users want to use this knob to
  	 * disable swapping for individual groups completely when
  	 * using the memory controller's swap limit feature would be
  	 * too expensive.
  	 */
02695175c   Johannes Weiner   mm: vmscan: move ...
2241
  	if (!global_reclaim(sc) && !swappiness) {
9a2651140   Johannes Weiner   mm: vmscan: clean...
2242
  		scan_balance = SCAN_FILE;
10316b313   Johannes Weiner   mm: vmscan: clari...
2243
2244
2245
2246
2247
2248
2249
2250
  		goto out;
  	}
  
  	/*
  	 * Do not apply any pressure balancing cleverness when the
  	 * system is close to OOM, scan both anon and file equally
  	 * (unless the swappiness setting disagrees with swapping).
  	 */
02695175c   Johannes Weiner   mm: vmscan: move ...
2251
  	if (!sc->priority && swappiness) {
9a2651140   Johannes Weiner   mm: vmscan: clean...
2252
  		scan_balance = SCAN_EQUAL;
10316b313   Johannes Weiner   mm: vmscan: clari...
2253
2254
  		goto out;
  	}
11d16c25b   Johannes Weiner   mm: vmscan: impro...
2255
  	/*
623762517   Johannes Weiner   revert "mm: vmsca...
2256
2257
2258
2259
2260
2261
2262
2263
2264
  	 * Prevent the reclaimer from falling into the cache trap: as
  	 * cache pages start out inactive, every cache fault will tip
  	 * the scan balance towards the file LRU.  And as the file LRU
  	 * shrinks, so does the window for rotation from references.
  	 * This means we have a runaway feedback loop where a tiny
  	 * thrashing file LRU becomes infinitely more attractive than
  	 * anon pages.  Try to detect this based on file LRU size.
  	 */
  	if (global_reclaim(sc)) {
599d0c954   Mel Gorman   mm, vmscan: move ...
2265
2266
2267
2268
  		unsigned long pgdatfile;
  		unsigned long pgdatfree;
  		int z;
  		unsigned long total_high_wmark = 0;
2ab051e11   Jerome Marchand   memcg, vmscan: Fi...
2269

599d0c954   Mel Gorman   mm, vmscan: move ...
2270
2271
2272
2273
2274
2275
  		pgdatfree = sum_zone_node_page_state(pgdat->node_id, NR_FREE_PAGES);
  		pgdatfile = node_page_state(pgdat, NR_ACTIVE_FILE) +
  			   node_page_state(pgdat, NR_INACTIVE_FILE);
  
  		for (z = 0; z < MAX_NR_ZONES; z++) {
  			struct zone *zone = &pgdat->node_zones[z];
6aa303def   Mel Gorman   mm, vmscan: only ...
2276
  			if (!managed_zone(zone))
599d0c954   Mel Gorman   mm, vmscan: move ...
2277
2278
2279
2280
  				continue;
  
  			total_high_wmark += high_wmark_pages(zone);
  		}
623762517   Johannes Weiner   revert "mm: vmsca...
2281

599d0c954   Mel Gorman   mm, vmscan: move ...
2282
  		if (unlikely(pgdatfile + pgdatfree <= total_high_wmark)) {
062262267   David Rientjes   mm, vmscan: avoid...
2283
2284
2285
2286
2287
  			/*
  			 * Force SCAN_ANON if there are enough inactive
  			 * anonymous pages on the LRU in eligible zones.
  			 * Otherwise, the small LRU gets thrashed.
  			 */
3b991208b   Johannes Weiner   mm: fix inactive ...
2288
  			if (!inactive_list_is_low(lruvec, false, sc, false) &&
062262267   David Rientjes   mm, vmscan: avoid...
2289
2290
2291
2292
2293
  			    lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, sc->reclaim_idx)
  					>> sc->priority) {
  				scan_balance = SCAN_ANON;
  				goto out;
  			}
623762517   Johannes Weiner   revert "mm: vmsca...
2294
2295
2296
2297
  		}
  	}
  
  	/*
316bda0e6   Vladimir Davydov   vmscan: do not fo...
2298
2299
2300
2301
2302
2303
2304
  	 * If there is enough inactive page cache, i.e. if the size of the
  	 * inactive list is greater than that of the active list *and* the
  	 * inactive list actually has some pages to scan on this priority, we
  	 * do not reclaim anything from the anonymous working set right now.
  	 * Without the second condition we could end up never scanning an
  	 * lruvec even if it has plenty of old anonymous pages unless the
  	 * system is under heavy pressure.
7c5bd705d   Johannes Weiner   mm: memcg: only e...
2305
  	 */
3b991208b   Johannes Weiner   mm: fix inactive ...
2306
  	if (!inactive_list_is_low(lruvec, true, sc, false) &&
71ab6cfe8   Michal Hocko   mm, vmscan: consi...
2307
  	    lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, sc->reclaim_idx) >> sc->priority) {
9a2651140   Johannes Weiner   mm: vmscan: clean...
2308
  		scan_balance = SCAN_FILE;
7c5bd705d   Johannes Weiner   mm: memcg: only e...
2309
2310
  		goto out;
  	}
9a2651140   Johannes Weiner   mm: vmscan: clean...
2311
  	scan_balance = SCAN_FRACT;
7c5bd705d   Johannes Weiner   mm: memcg: only e...
2312
  	/*
58c37f6e0   KOSAKI Motohiro   vmscan: protect r...
2313
2314
2315
  	 * With swappiness at 100, anonymous and file have the same priority.
  	 * This scanning priority is essentially the inverse of IO cost.
  	 */
02695175c   Johannes Weiner   mm: vmscan: move ...
2316
  	anon_prio = swappiness;
75b00af77   Hugh Dickins   mm: trivial clean...
2317
  	file_prio = 200 - anon_prio;
58c37f6e0   KOSAKI Motohiro   vmscan: protect r...
2318
2319
  
  	/*
4f98a2fee   Rik van Riel   vmscan: split LRU...
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
  	 * OK, so we have swap space and a fair amount of page cache
  	 * pages.  We use the recently rotated / recently scanned
  	 * ratios to determine how valuable each cache is.
  	 *
  	 * Because workloads change over time (and to avoid overflow)
  	 * we keep these statistics as a floating average, which ends
  	 * up weighing recent references more than old ones.
  	 *
  	 * anon in [0], file in [1]
  	 */
2ab051e11   Jerome Marchand   memcg, vmscan: Fi...
2330

fd5388037   Michal Hocko   mm, vmscan: clean...
2331
2332
2333
2334
  	anon  = lruvec_lru_size(lruvec, LRU_ACTIVE_ANON, MAX_NR_ZONES) +
  		lruvec_lru_size(lruvec, LRU_INACTIVE_ANON, MAX_NR_ZONES);
  	file  = lruvec_lru_size(lruvec, LRU_ACTIVE_FILE, MAX_NR_ZONES) +
  		lruvec_lru_size(lruvec, LRU_INACTIVE_FILE, MAX_NR_ZONES);
2ab051e11   Jerome Marchand   memcg, vmscan: Fi...
2335

599d0c954   Mel Gorman   mm, vmscan: move ...
2336
  	spin_lock_irq(&pgdat->lru_lock);
6e9015716   KOSAKI Motohiro   mm: introduce zon...
2337
  	if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) {
6e9015716   KOSAKI Motohiro   mm: introduce zon...
2338
2339
  		reclaim_stat->recent_scanned[0] /= 2;
  		reclaim_stat->recent_rotated[0] /= 2;
4f98a2fee   Rik van Riel   vmscan: split LRU...
2340
  	}
6e9015716   KOSAKI Motohiro   mm: introduce zon...
2341
  	if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) {
6e9015716   KOSAKI Motohiro   mm: introduce zon...
2342
2343
  		reclaim_stat->recent_scanned[1] /= 2;
  		reclaim_stat->recent_rotated[1] /= 2;
4f98a2fee   Rik van Riel   vmscan: split LRU...
2344
2345
2346
  	}
  
  	/*
00d8089c5   Rik van Riel   vmscan: fix get_s...
2347
2348
2349
  	 * The amount of pressure on anon vs file pages is inversely
  	 * proportional to the fraction of recently scanned pages on
  	 * each list that were recently referenced and in active use.
4f98a2fee   Rik van Riel   vmscan: split LRU...
2350
  	 */
fe35004fb   Satoru Moriya   mm: avoid swappin...
2351
  	ap = anon_prio * (reclaim_stat->recent_scanned[0] + 1);
6e9015716   KOSAKI Motohiro   mm: introduce zon...
2352
  	ap /= reclaim_stat->recent_rotated[0] + 1;
4f98a2fee   Rik van Riel   vmscan: split LRU...
2353

fe35004fb   Satoru Moriya   mm: avoid swappin...
2354
  	fp = file_prio * (reclaim_stat->recent_scanned[1] + 1);
6e9015716   KOSAKI Motohiro   mm: introduce zon...
2355
  	fp /= reclaim_stat->recent_rotated[1] + 1;
599d0c954   Mel Gorman   mm, vmscan: move ...
2356
  	spin_unlock_irq(&pgdat->lru_lock);
4f98a2fee   Rik van Riel   vmscan: split LRU...
2357

76a33fc38   Shaohua Li   vmscan: prevent g...
2358
2359
2360
2361
  	fraction[0] = ap;
  	fraction[1] = fp;
  	denominator = ap + fp + 1;
  out:
688035f72   Johannes Weiner   mm: don't avoid h...
2362
2363
2364
  	*lru_pages = 0;
  	for_each_evictable_lru(lru) {
  		int file = is_file_lru(lru);
9783aa991   Chris Down   mm, memcg: propor...
2365
  		unsigned long lruvec_size;
688035f72   Johannes Weiner   mm: don't avoid h...
2366
  		unsigned long scan;
1bc63fb12   Chris Down   mm, memcg: make s...
2367
  		unsigned long protection;
9783aa991   Chris Down   mm, memcg: propor...
2368
2369
  
  		lruvec_size = lruvec_lru_size(lruvec, lru, sc->reclaim_idx);
1bc63fb12   Chris Down   mm, memcg: make s...
2370
2371
  		protection = mem_cgroup_protection(memcg,
  						   sc->memcg_low_reclaim);
9783aa991   Chris Down   mm, memcg: propor...
2372

1bc63fb12   Chris Down   mm, memcg: make s...
2373
  		if (protection) {
9783aa991   Chris Down   mm, memcg: propor...
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
  			/*
  			 * Scale a cgroup's reclaim pressure by proportioning
  			 * its current usage to its memory.low or memory.min
  			 * setting.
  			 *
  			 * This is important, as otherwise scanning aggression
  			 * becomes extremely binary -- from nothing as we
  			 * approach the memory protection threshold, to totally
  			 * nominal as we exceed it.  This results in requiring
  			 * setting extremely liberal protection thresholds. It
  			 * also means we simply get no protection at all if we
  			 * set it too low, which is not ideal.
1bc63fb12   Chris Down   mm, memcg: make s...
2386
2387
2388
2389
  			 *
  			 * If there is any protection in place, we reduce scan
  			 * pressure by how much of the total memory used is
  			 * within protection thresholds.
9783aa991   Chris Down   mm, memcg: propor...
2390
  			 *
9de7ca46a   Chris Down   mm, memcg: make m...
2391
2392
2393
2394
2395
2396
2397
2398
  			 * There is one special case: in the first reclaim pass,
  			 * we skip over all groups that are within their low
  			 * protection. If that fails to reclaim enough pages to
  			 * satisfy the reclaim goal, we come back and override
  			 * the best-effort low protection. However, we still
  			 * ideally want to honor how well-behaved groups are in
  			 * that case instead of simply punishing them all
  			 * equally. As such, we reclaim them based on how much
1bc63fb12   Chris Down   mm, memcg: make s...
2399
2400
2401
  			 * memory they are using, reducing the scan pressure
  			 * again by how much of the total memory used is under
  			 * hard protection.
9783aa991   Chris Down   mm, memcg: propor...
2402
  			 */
1bc63fb12   Chris Down   mm, memcg: make s...
2403
2404
2405
2406
2407
2408
2409
  			unsigned long cgroup_size = mem_cgroup_size(memcg);
  
  			/* Avoid TOCTOU with earlier protection check */
  			cgroup_size = max(cgroup_size, protection);
  
  			scan = lruvec_size - lruvec_size * protection /
  				cgroup_size;
9783aa991   Chris Down   mm, memcg: propor...
2410
2411
  
  			/*
1bc63fb12   Chris Down   mm, memcg: make s...
2412
  			 * Minimally target SWAP_CLUSTER_MAX pages to keep
9de7ca46a   Chris Down   mm, memcg: make m...
2413
2414
  			 * reclaim moving forwards, avoiding decremeting
  			 * sc->priority further than desirable.
9783aa991   Chris Down   mm, memcg: propor...
2415
  			 */
1bc63fb12   Chris Down   mm, memcg: make s...
2416
  			scan = max(scan, SWAP_CLUSTER_MAX);
9783aa991   Chris Down   mm, memcg: propor...
2417
2418
2419
2420
2421
  		} else {
  			scan = lruvec_size;
  		}
  
  		scan >>= sc->priority;
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
2422

688035f72   Johannes Weiner   mm: don't avoid h...
2423
2424
2425
2426
2427
  		/*
  		 * If the cgroup's already been deleted, make sure to
  		 * scrape out the remaining cache.
  		 */
  		if (!scan && !mem_cgroup_online(memcg))
9783aa991   Chris Down   mm, memcg: propor...
2428
  			scan = min(lruvec_size, SWAP_CLUSTER_MAX);
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
2429

688035f72   Johannes Weiner   mm: don't avoid h...
2430
2431
2432
2433
2434
  		switch (scan_balance) {
  		case SCAN_EQUAL:
  			/* Scan lists relative to size */
  			break;
  		case SCAN_FRACT:
9a2651140   Johannes Weiner   mm: vmscan: clean...
2435
  			/*
688035f72   Johannes Weiner   mm: don't avoid h...
2436
2437
  			 * Scan types proportional to swappiness and
  			 * their relative recent reclaim efficiency.
198f5aa0f   Gavin Shan   mm/vmscan.c: don'...
2438
2439
2440
  			 * Make sure we don't miss the last page on
  			 * the offlined memory cgroups because of a
  			 * round-off error.
9a2651140   Johannes Weiner   mm: vmscan: clean...
2441
  			 */
198f5aa0f   Gavin Shan   mm/vmscan.c: don'...
2442
2443
2444
  			scan = mem_cgroup_online(memcg) ?
  			       div64_u64(scan * fraction[file], denominator) :
  			       DIV64_U64_ROUND_UP(scan * fraction[file],
68600f623   Roman Gushchin   mm: don't miss th...
2445
  						  denominator);
688035f72   Johannes Weiner   mm: don't avoid h...
2446
2447
2448
2449
2450
  			break;
  		case SCAN_FILE:
  		case SCAN_ANON:
  			/* Scan one type exclusively */
  			if ((scan_balance == SCAN_FILE) != file) {
9783aa991   Chris Down   mm, memcg: propor...
2451
  				lruvec_size = 0;
688035f72   Johannes Weiner   mm: don't avoid h...
2452
2453
2454
2455
2456
2457
  				scan = 0;
  			}
  			break;
  		default:
  			/* Look ma, no brain */
  			BUG();
9a2651140   Johannes Weiner   mm: vmscan: clean...
2458
  		}
688035f72   Johannes Weiner   mm: don't avoid h...
2459

9783aa991   Chris Down   mm, memcg: propor...
2460
  		*lru_pages += lruvec_size;
688035f72   Johannes Weiner   mm: don't avoid h...
2461
  		nr[lru] = scan;
76a33fc38   Shaohua Li   vmscan: prevent g...
2462
  	}
6e08a369e   Wu Fengguang   vmscan: cleanup t...
2463
  }
4f98a2fee   Rik van Riel   vmscan: split LRU...
2464

9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2465
  /*
a9dd0a831   Mel Gorman   mm, vmscan: make ...
2466
   * This is a basic per-node page freer.  Used by both kswapd and direct reclaim.
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2467
   */
a9dd0a831   Mel Gorman   mm, vmscan: make ...
2468
  static void shrink_node_memcg(struct pglist_data *pgdat, struct mem_cgroup *memcg,
333776785   Vladimir Davydov   mm: vmscan: pass ...
2469
  			      struct scan_control *sc, unsigned long *lru_pages)
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2470
  {
ef8f23279   Mel Gorman   mm, memcg: move m...
2471
  	struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2472
  	unsigned long nr[NR_LRU_LISTS];
e82e0561d   Mel Gorman   mm: vmscan: obey ...
2473
  	unsigned long targets[NR_LRU_LISTS];
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2474
2475
2476
2477
2478
  	unsigned long nr_to_scan;
  	enum lru_list lru;
  	unsigned long nr_reclaimed = 0;
  	unsigned long nr_to_reclaim = sc->nr_to_reclaim;
  	struct blk_plug plug;
1a501907b   Mel Gorman   mm: vmscan: use p...
2479
  	bool scan_adjusted;
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2480

333776785   Vladimir Davydov   mm: vmscan: pass ...
2481
  	get_scan_count(lruvec, memcg, sc, nr, lru_pages);
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2482

e82e0561d   Mel Gorman   mm: vmscan: obey ...
2483
2484
  	/* Record the original scan target for proportional adjustments later */
  	memcpy(targets, nr, sizeof(nr));
1a501907b   Mel Gorman   mm: vmscan: use p...
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
  	/*
  	 * Global reclaiming within direct reclaim at DEF_PRIORITY is a normal
  	 * event that can occur when there is little memory pressure e.g.
  	 * multiple streaming readers/writers. Hence, we do not abort scanning
  	 * when the requested number of pages are reclaimed when scanning at
  	 * DEF_PRIORITY on the assumption that the fact we are direct
  	 * reclaiming implies that kswapd is not keeping up and it is best to
  	 * do a batch of work at once. For memcg reclaim one check is made to
  	 * abort proportional reclaim if either the file or anon lru has already
  	 * dropped to zero at the first pass.
  	 */
  	scan_adjusted = (global_reclaim(sc) && !current_is_kswapd() &&
  			 sc->priority == DEF_PRIORITY);
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2498
2499
2500
  	blk_start_plug(&plug);
  	while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] ||
  					nr[LRU_INACTIVE_FILE]) {
e82e0561d   Mel Gorman   mm: vmscan: obey ...
2501
2502
  		unsigned long nr_anon, nr_file, percentage;
  		unsigned long nr_scanned;
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2503
2504
2505
2506
2507
2508
  		for_each_evictable_lru(lru) {
  			if (nr[lru]) {
  				nr_to_scan = min(nr[lru], SWAP_CLUSTER_MAX);
  				nr[lru] -= nr_to_scan;
  
  				nr_reclaimed += shrink_list(lru, nr_to_scan,
3b991208b   Johannes Weiner   mm: fix inactive ...
2509
  							    lruvec, sc);
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2510
2511
  			}
  		}
e82e0561d   Mel Gorman   mm: vmscan: obey ...
2512

bd041733c   Michal Hocko   mm, vmscan: add c...
2513
  		cond_resched();
e82e0561d   Mel Gorman   mm: vmscan: obey ...
2514
2515
  		if (nr_reclaimed < nr_to_reclaim || scan_adjusted)
  			continue;
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2516
  		/*
e82e0561d   Mel Gorman   mm: vmscan: obey ...
2517
  		 * For kswapd and memcg, reclaim at least the number of pages
1a501907b   Mel Gorman   mm: vmscan: use p...
2518
  		 * requested. Ensure that the anon and file LRUs are scanned
e82e0561d   Mel Gorman   mm: vmscan: obey ...
2519
2520
2521
2522
2523
2524
  		 * proportionally what was requested by get_scan_count(). We
  		 * stop reclaiming one LRU and reduce the amount scanning
  		 * proportional to the original scan target.
  		 */
  		nr_file = nr[LRU_INACTIVE_FILE] + nr[LRU_ACTIVE_FILE];
  		nr_anon = nr[LRU_INACTIVE_ANON] + nr[LRU_ACTIVE_ANON];
1a501907b   Mel Gorman   mm: vmscan: use p...
2525
2526
2527
2528
2529
2530
2531
2532
  		/*
  		 * It's just vindictive to attack the larger once the smaller
  		 * has gone to zero.  And given the way we stop scanning the
  		 * smaller below, this makes sure that we only make one nudge
  		 * towards proportionality once we've got nr_to_reclaim.
  		 */
  		if (!nr_file || !nr_anon)
  			break;
e82e0561d   Mel Gorman   mm: vmscan: obey ...
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
  		if (nr_file > nr_anon) {
  			unsigned long scan_target = targets[LRU_INACTIVE_ANON] +
  						targets[LRU_ACTIVE_ANON] + 1;
  			lru = LRU_BASE;
  			percentage = nr_anon * 100 / scan_target;
  		} else {
  			unsigned long scan_target = targets[LRU_INACTIVE_FILE] +
  						targets[LRU_ACTIVE_FILE] + 1;
  			lru = LRU_FILE;
  			percentage = nr_file * 100 / scan_target;
  		}
  
  		/* Stop scanning the smaller of the LRU */
  		nr[lru] = 0;
  		nr[lru + LRU_ACTIVE] = 0;
  
  		/*
  		 * Recalculate the other LRU scan count based on its original
  		 * scan target and the percentage scanning already complete
  		 */
  		lru = (lru == LRU_FILE) ? LRU_BASE : LRU_FILE;
  		nr_scanned = targets[lru] - nr[lru];
  		nr[lru] = targets[lru] * (100 - percentage) / 100;
  		nr[lru] -= min(nr[lru], nr_scanned);
  
  		lru += LRU_ACTIVE;
  		nr_scanned = targets[lru] - nr[lru];
  		nr[lru] = targets[lru] * (100 - percentage) / 100;
  		nr[lru] -= min(nr[lru], nr_scanned);
  
  		scan_adjusted = true;
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2564
2565
2566
2567
2568
2569
2570
2571
  	}
  	blk_finish_plug(&plug);
  	sc->nr_reclaimed += nr_reclaimed;
  
  	/*
  	 * Even if we did not try to evict anon pages at all, we want to
  	 * rebalance the anon lru active/inactive ratio.
  	 */
3b991208b   Johannes Weiner   mm: fix inactive ...
2572
  	if (inactive_list_is_low(lruvec, false, sc, true))
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2573
2574
  		shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
  				   sc, LRU_ACTIVE_ANON);
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2575
  }
23b9da55c   Mel Gorman   mm: vmscan: remov...
2576
  /* Use reclaim/compaction for costly allocs or under memory pressure */
9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
2577
  static bool in_reclaim_compaction(struct scan_control *sc)
23b9da55c   Mel Gorman   mm: vmscan: remov...
2578
  {
d84da3f9e   Kirill A. Shutemov   mm: use IS_ENABLE...
2579
  	if (IS_ENABLED(CONFIG_COMPACTION) && sc->order &&
23b9da55c   Mel Gorman   mm: vmscan: remov...
2580
  			(sc->order > PAGE_ALLOC_COSTLY_ORDER ||
9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
2581
  			 sc->priority < DEF_PRIORITY - 2))
23b9da55c   Mel Gorman   mm: vmscan: remov...
2582
2583
2584
2585
  		return true;
  
  	return false;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2586
  /*
23b9da55c   Mel Gorman   mm: vmscan: remov...
2587
2588
2589
2590
2591
   * Reclaim/compaction is used for high-order allocation requests. It reclaims
   * order-0 pages before compacting the zone. should_continue_reclaim() returns
   * true if more pages should be reclaimed such that when the page allocator
   * calls try_to_compact_zone() that it will have enough free pages to succeed.
   * It will give up earlier than that if there is difficulty reclaiming pages.
3e7d34497   Mel Gorman   mm: vmscan: recla...
2592
   */
a9dd0a831   Mel Gorman   mm, vmscan: make ...
2593
  static inline bool should_continue_reclaim(struct pglist_data *pgdat,
3e7d34497   Mel Gorman   mm: vmscan: recla...
2594
  					unsigned long nr_reclaimed,
3e7d34497   Mel Gorman   mm: vmscan: recla...
2595
2596
2597
2598
  					struct scan_control *sc)
  {
  	unsigned long pages_for_compaction;
  	unsigned long inactive_lru_pages;
a9dd0a831   Mel Gorman   mm, vmscan: make ...
2599
  	int z;
3e7d34497   Mel Gorman   mm: vmscan: recla...
2600
2601
  
  	/* If not in reclaim/compaction mode, stop */
9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
2602
  	if (!in_reclaim_compaction(sc))
3e7d34497   Mel Gorman   mm: vmscan: recla...
2603
  		return false;
5ee04716c   Vlastimil Babka   mm, reclaim: clea...
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
  	/*
  	 * Stop if we failed to reclaim any pages from the last SWAP_CLUSTER_MAX
  	 * number of pages that were scanned. This will return to the caller
  	 * with the risk reclaim/compaction and the resulting allocation attempt
  	 * fails. In the past we have tried harder for __GFP_RETRY_MAYFAIL
  	 * allocations through requiring that the full LRU list has been scanned
  	 * first, by assuming that zero delta of sc->nr_scanned means full LRU
  	 * scan, but that approximation was wrong, and there were corner cases
  	 * where always a non-zero amount of pages were scanned.
  	 */
  	if (!nr_reclaimed)
  		return false;
3e7d34497   Mel Gorman   mm: vmscan: recla...
2616

3e7d34497   Mel Gorman   mm: vmscan: recla...
2617
  	/* If compaction would go ahead or the allocation would succeed, stop */
a9dd0a831   Mel Gorman   mm, vmscan: make ...
2618
2619
  	for (z = 0; z <= sc->reclaim_idx; z++) {
  		struct zone *zone = &pgdat->node_zones[z];
6aa303def   Mel Gorman   mm, vmscan: only ...
2620
  		if (!managed_zone(zone))
a9dd0a831   Mel Gorman   mm, vmscan: make ...
2621
2622
2623
  			continue;
  
  		switch (compaction_suitable(zone, sc->order, 0, sc->reclaim_idx)) {
cf378319d   Vlastimil Babka   mm, compaction: r...
2624
  		case COMPACT_SUCCESS:
a9dd0a831   Mel Gorman   mm, vmscan: make ...
2625
2626
2627
2628
2629
2630
  		case COMPACT_CONTINUE:
  			return false;
  		default:
  			/* check next zone */
  			;
  		}
3e7d34497   Mel Gorman   mm: vmscan: recla...
2631
  	}
1c6c15971   Hillf Danton   mm, reclaim: make...
2632
2633
2634
2635
2636
2637
2638
2639
2640
  
  	/*
  	 * If we have not reclaimed enough pages for compaction and the
  	 * inactive lists are large enough, continue reclaiming
  	 */
  	pages_for_compaction = compact_gap(sc->order);
  	inactive_lru_pages = node_page_state(pgdat, NR_INACTIVE_FILE);
  	if (get_nr_swap_pages() > 0)
  		inactive_lru_pages += node_page_state(pgdat, NR_INACTIVE_ANON);
5ee04716c   Vlastimil Babka   mm, reclaim: clea...
2641
  	return inactive_lru_pages > pages_for_compaction;
3e7d34497   Mel Gorman   mm: vmscan: recla...
2642
  }
e3c1ac586   Andrey Ryabinin   mm/vmscan: don't ...
2643
2644
2645
2646
2647
  static bool pgdat_memcg_congested(pg_data_t *pgdat, struct mem_cgroup *memcg)
  {
  	return test_bit(PGDAT_CONGESTED, &pgdat->flags) ||
  		(memcg && memcg_congested(pgdat, memcg));
  }
970a39a36   Mel Gorman   mm, vmscan: avoid...
2648
  static bool shrink_node(pg_data_t *pgdat, struct scan_control *sc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2649
  {
cb731d6c6   Vladimir Davydov   vmscan: per memor...
2650
  	struct reclaim_state *reclaim_state = current->reclaim_state;
f0fdc5e8e   Johannes Weiner   vmscan: fix zone ...
2651
  	unsigned long nr_reclaimed, nr_scanned;
2344d7e44   Johannes Weiner   mm: vmscan: remov...
2652
  	bool reclaimable = false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2653

9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2654
2655
  	do {
  		struct mem_cgroup *root = sc->target_mem_cgroup;
a9dd0a831   Mel Gorman   mm, vmscan: make ...
2656
  		unsigned long node_lru_pages = 0;
694fbc0fe   Andrew Morton   revert "memcg: en...
2657
  		struct mem_cgroup *memcg;
3e7d34497   Mel Gorman   mm: vmscan: recla...
2658

d108c7721   Andrey Ryabinin   mm/vmscan: don't ...
2659
  		memset(&sc->nr, 0, sizeof(sc->nr));
9b4f98cda   Johannes Weiner   mm: vmscan: compa...
2660
2661
  		nr_reclaimed = sc->nr_reclaimed;
  		nr_scanned = sc->nr_scanned;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2662

1ba6fc9af   Johannes Weiner   mm: vmscan: do no...
2663
  		memcg = mem_cgroup_iter(root, NULL, NULL);
694fbc0fe   Andrew Morton   revert "memcg: en...
2664
  		do {
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
2665
  			unsigned long lru_pages;
8e8ae6452   Johannes Weiner   mm: memcontrol: h...
2666
  			unsigned long reclaimed;
cb731d6c6   Vladimir Davydov   vmscan: per memor...
2667
  			unsigned long scanned;
5660048cc   Johannes Weiner   mm: move memcg hi...
2668

a5bc1c7a9   Xunlei Pang   mm: memcg: fix me...
2669
2670
2671
2672
2673
2674
2675
  			/*
  			 * This loop can become CPU-bound when target memcgs
  			 * aren't eligible for reclaim - either because they
  			 * don't have any reclaimable pages, or because their
  			 * memory is explicitly protected. Avoid soft lockups.
  			 */
  			cond_resched();
bf8d5d52f   Roman Gushchin   memcg: introduce ...
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
  			switch (mem_cgroup_protected(root, memcg)) {
  			case MEMCG_PROT_MIN:
  				/*
  				 * Hard protection.
  				 * If there is no reclaimable memory, OOM.
  				 */
  				continue;
  			case MEMCG_PROT_LOW:
  				/*
  				 * Soft protection.
  				 * Respect the protection only as long as
  				 * there is an unprotected supply
  				 * of reclaimable memory from other cgroups.
  				 */
d6622f636   Yisheng Xie   mm/vmscan: more r...
2690
2691
  				if (!sc->memcg_low_reclaim) {
  					sc->memcg_low_skipped = 1;
241994ed8   Johannes Weiner   mm: memcontrol: d...
2692
  					continue;
d6622f636   Yisheng Xie   mm/vmscan: more r...
2693
  				}
e27be240d   Johannes Weiner   mm: memcg: make s...
2694
  				memcg_memory_event(memcg, MEMCG_LOW);
bf8d5d52f   Roman Gushchin   memcg: introduce ...
2695
2696
  				break;
  			case MEMCG_PROT_NONE:
9783aa991   Chris Down   mm, memcg: propor...
2697
2698
2699
2700
2701
2702
2703
  				/*
  				 * All protection thresholds breached. We may
  				 * still choose to vary the scan pressure
  				 * applied based on by how much the cgroup in
  				 * question has exceeded its protection
  				 * thresholds (see get_scan_count).
  				 */
bf8d5d52f   Roman Gushchin   memcg: introduce ...
2704
  				break;
241994ed8   Johannes Weiner   mm: memcontrol: d...
2705
  			}
8e8ae6452   Johannes Weiner   mm: memcontrol: h...
2706
  			reclaimed = sc->nr_reclaimed;
cb731d6c6   Vladimir Davydov   vmscan: per memor...
2707
  			scanned = sc->nr_scanned;
a9dd0a831   Mel Gorman   mm, vmscan: make ...
2708
2709
  			shrink_node_memcg(pgdat, memcg, sc, &lru_pages);
  			node_lru_pages += lru_pages;
f16015fbf   Johannes Weiner   mm: vmscan: disti...
2710

28360f398   Mel Gorman   mm, vmscan: do no...
2711
2712
  			shrink_slab(sc->gfp_mask, pgdat->node_id, memcg,
  					sc->priority);
cb731d6c6   Vladimir Davydov   vmscan: per memor...
2713

8e8ae6452   Johannes Weiner   mm: memcontrol: h...
2714
2715
2716
2717
  			/* Record the group's reclaim efficiency */
  			vmpressure(sc->gfp_mask, memcg, false,
  				   sc->nr_scanned - scanned,
  				   sc->nr_reclaimed - reclaimed);
1ba6fc9af   Johannes Weiner   mm: vmscan: do no...
2718
  		} while ((memcg = mem_cgroup_iter(root, memcg, NULL)));
70ddf637e   Anton Vorontsov   memcg: add memory...
2719

cb731d6c6   Vladimir Davydov   vmscan: per memor...
2720
2721
2722
  		if (reclaim_state) {
  			sc->nr_reclaimed += reclaim_state->reclaimed_slab;
  			reclaim_state->reclaimed_slab = 0;
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
2723
  		}
8e8ae6452   Johannes Weiner   mm: memcontrol: h...
2724
2725
  		/* Record the subtree's reclaim efficiency */
  		vmpressure(sc->gfp_mask, sc->target_mem_cgroup, true,
70ddf637e   Anton Vorontsov   memcg: add memory...
2726
2727
  			   sc->nr_scanned - nr_scanned,
  			   sc->nr_reclaimed - nr_reclaimed);
2344d7e44   Johannes Weiner   mm: vmscan: remov...
2728
2729
  		if (sc->nr_reclaimed - nr_reclaimed)
  			reclaimable = true;
e3c1ac586   Andrey Ryabinin   mm/vmscan: don't ...
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
  		if (current_is_kswapd()) {
  			/*
  			 * If reclaim is isolating dirty pages under writeback,
  			 * it implies that the long-lived page allocation rate
  			 * is exceeding the page laundering rate. Either the
  			 * global limits are not being effective at throttling
  			 * processes due to the page distribution throughout
  			 * zones or there is heavy usage of a slow backing
  			 * device. The only option is to throttle from reclaim
  			 * context which is not ideal as there is no guarantee
  			 * the dirtying process is throttled in the same way
  			 * balance_dirty_pages() manages.
  			 *
  			 * Once a node is flagged PGDAT_WRITEBACK, kswapd will
  			 * count the number of pages under pages flagged for
  			 * immediate reclaim and stall if any are encountered
  			 * in the nr_immediate check below.
  			 */
  			if (sc->nr.writeback && sc->nr.writeback == sc->nr.taken)
  				set_bit(PGDAT_WRITEBACK, &pgdat->flags);
d108c7721   Andrey Ryabinin   mm/vmscan: don't ...
2750

d108c7721   Andrey Ryabinin   mm/vmscan: don't ...
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
  			/*
  			 * Tag a node as congested if all the dirty pages
  			 * scanned were backed by a congested BDI and
  			 * wait_iff_congested will stall.
  			 */
  			if (sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
  				set_bit(PGDAT_CONGESTED, &pgdat->flags);
  
  			/* Allow kswapd to start writing pages during reclaim.*/
  			if (sc->nr.unqueued_dirty == sc->nr.file_taken)
  				set_bit(PGDAT_DIRTY, &pgdat->flags);
  
  			/*
  			 * If kswapd scans pages marked marked for immediate
  			 * reclaim and under writeback (nr_immediate), it
  			 * implies that pages are cycling through the LRU
  			 * faster than they are written so also forcibly stall.
  			 */
  			if (sc->nr.immediate)
  				congestion_wait(BLK_RW_ASYNC, HZ/10);
  		}
  
  		/*
e3c1ac586   Andrey Ryabinin   mm/vmscan: don't ...
2774
2775
2776
2777
2778
2779
2780
2781
  		 * Legacy memcg will stall in page writeback so avoid forcibly
  		 * stalling in wait_iff_congested().
  		 */
  		if (!global_reclaim(sc) && sane_reclaim(sc) &&
  		    sc->nr.dirty && sc->nr.dirty == sc->nr.congested)
  			set_memcg_congestion(pgdat, root, true);
  
  		/*
d108c7721   Andrey Ryabinin   mm/vmscan: don't ...
2782
2783
2784
2785
2786
2787
  		 * Stall direct reclaim for IO completions if underlying BDIs
  		 * and node is congested. Allow kswapd to continue until it
  		 * starts encountering unqueued dirty pages or cycling through
  		 * the LRU too quickly.
  		 */
  		if (!sc->hibernation_mode && !current_is_kswapd() &&
e3c1ac586   Andrey Ryabinin   mm/vmscan: don't ...
2788
2789
  		   current_may_throttle() && pgdat_memcg_congested(pgdat, root))
  			wait_iff_congested(BLK_RW_ASYNC, HZ/10);
d108c7721   Andrey Ryabinin   mm/vmscan: don't ...
2790

a9dd0a831   Mel Gorman   mm, vmscan: make ...
2791
  	} while (should_continue_reclaim(pgdat, sc->nr_reclaimed - nr_reclaimed,
5ee04716c   Vlastimil Babka   mm, reclaim: clea...
2792
  					 sc));
2344d7e44   Johannes Weiner   mm: vmscan: remov...
2793

c73322d09   Johannes Weiner   mm: fix 100% CPU ...
2794
2795
2796
2797
2798
2799
2800
2801
  	/*
  	 * Kswapd gives up on balancing particular nodes after too
  	 * many failures to reclaim anything from them and goes to
  	 * sleep. On reclaim progress, reset the failure counter. A
  	 * successful direct reclaim run will revive a dormant kswapd.
  	 */
  	if (reclaimable)
  		pgdat->kswapd_failures = 0;
2344d7e44   Johannes Weiner   mm: vmscan: remov...
2802
  	return reclaimable;
f16015fbf   Johannes Weiner   mm: vmscan: disti...
2803
  }
53853e2d2   Vlastimil Babka   mm, compaction: d...
2804
  /*
fdd4c6149   Vlastimil Babka   mm, vmscan: make ...
2805
2806
2807
   * Returns true if compaction should go ahead for a costly-order request, or
   * the allocation would already succeed without compaction. Return false if we
   * should reclaim first.
53853e2d2   Vlastimil Babka   mm, compaction: d...
2808
   */
4f588331b   Mel Gorman   mm, vmscan: avoid...
2809
  static inline bool compaction_ready(struct zone *zone, struct scan_control *sc)
fe4b1b244   Mel Gorman   mm: vmscan: when ...
2810
  {
31483b6ad   Mel Gorman   mm, vmscan: remov...
2811
  	unsigned long watermark;
fdd4c6149   Vlastimil Babka   mm, vmscan: make ...
2812
  	enum compact_result suitable;
fe4b1b244   Mel Gorman   mm: vmscan: when ...
2813

fdd4c6149   Vlastimil Babka   mm, vmscan: make ...
2814
2815
2816
2817
2818
2819
2820
  	suitable = compaction_suitable(zone, sc->order, 0, sc->reclaim_idx);
  	if (suitable == COMPACT_SUCCESS)
  		/* Allocation should succeed already. Don't reclaim. */
  		return true;
  	if (suitable == COMPACT_SKIPPED)
  		/* Compaction cannot yet proceed. Do reclaim. */
  		return false;
fe4b1b244   Mel Gorman   mm: vmscan: when ...
2821

53853e2d2   Vlastimil Babka   mm, compaction: d...
2822
  	/*
fdd4c6149   Vlastimil Babka   mm, vmscan: make ...
2823
2824
2825
2826
2827
2828
2829
  	 * Compaction is already possible, but it takes time to run and there
  	 * are potentially other callers using the pages just freed. So proceed
  	 * with reclaim to make a buffer of free pages available to give
  	 * compaction a reasonable chance of completing and allocating the page.
  	 * Note that we won't actually reclaim the whole buffer in one attempt
  	 * as the target watermark in should_continue_reclaim() is lower. But if
  	 * we are already above the high+gap watermark, don't reclaim at all.
53853e2d2   Vlastimil Babka   mm, compaction: d...
2830
  	 */
fdd4c6149   Vlastimil Babka   mm, vmscan: make ...
2831
  	watermark = high_wmark_pages(zone) + compact_gap(sc->order);
fe4b1b244   Mel Gorman   mm: vmscan: when ...
2832

fdd4c6149   Vlastimil Babka   mm, vmscan: make ...
2833
  	return zone_watermark_ok_safe(zone, 0, watermark, sc->reclaim_idx);
fe4b1b244   Mel Gorman   mm: vmscan: when ...
2834
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2835
2836
2837
2838
2839
  /*
   * This is the direct reclaim path, for page-allocating processes.  We only
   * try to reclaim pages from zones which will satisfy the caller's allocation
   * request.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2840
2841
2842
   * If a zone is deemed to be full of pinned pages then just give it a light
   * scan then give up on it.
   */
0a0337e0d   Michal Hocko   mm, oom: rework o...
2843
  static void shrink_zones(struct zonelist *zonelist, struct scan_control *sc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2844
  {
dd1a239f6   Mel Gorman   mm: have zonelist...
2845
  	struct zoneref *z;
54a6eb5c4   Mel Gorman   mm: use two zonel...
2846
  	struct zone *zone;
0608f43da   Andrew Morton   revert "memcg, vm...
2847
2848
  	unsigned long nr_soft_reclaimed;
  	unsigned long nr_soft_scanned;
619d0d76c   Weijie Yang   mm/vmscan: restor...
2849
  	gfp_t orig_mask;
79dafcdca   Mel Gorman   mm, vmscan: by de...
2850
  	pg_data_t *last_pgdat = NULL;
1cfb419b3   KAMEZAWA Hiroyuki   per-zone and recl...
2851

cc715d99e   Mel Gorman   mm: vmscan: forci...
2852
2853
2854
2855
2856
  	/*
  	 * If the number of buffer_heads in the machine exceeds the maximum
  	 * allowed level, force direct reclaim to scan the highmem zone as
  	 * highmem pages could be pinning lowmem pages storing buffer_heads
  	 */
619d0d76c   Weijie Yang   mm/vmscan: restor...
2857
  	orig_mask = sc->gfp_mask;
b2e18757f   Mel Gorman   mm, vmscan: begin...
2858
  	if (buffer_heads_over_limit) {
cc715d99e   Mel Gorman   mm: vmscan: forci...
2859
  		sc->gfp_mask |= __GFP_HIGHMEM;
4f588331b   Mel Gorman   mm, vmscan: avoid...
2860
  		sc->reclaim_idx = gfp_zone(sc->gfp_mask);
b2e18757f   Mel Gorman   mm, vmscan: begin...
2861
  	}
cc715d99e   Mel Gorman   mm: vmscan: forci...
2862

d4debc66d   Mel Gorman   vmscan: remove un...
2863
  	for_each_zone_zonelist_nodemask(zone, z, zonelist,
b2e18757f   Mel Gorman   mm, vmscan: begin...
2864
  					sc->reclaim_idx, sc->nodemask) {
b2e18757f   Mel Gorman   mm, vmscan: begin...
2865
  		/*
1cfb419b3   KAMEZAWA Hiroyuki   per-zone and recl...
2866
2867
2868
  		 * Take care memory controller reclaiming has small influence
  		 * to global LRU.
  		 */
89b5fae53   Johannes Weiner   mm: vmscan: disti...
2869
  		if (global_reclaim(sc)) {
344736f29   Vladimir Davydov   cpuset: simplify ...
2870
2871
  			if (!cpuset_zone_allowed(zone,
  						 GFP_KERNEL | __GFP_HARDWALL))
1cfb419b3   KAMEZAWA Hiroyuki   per-zone and recl...
2872
  				continue;
65ec02cb9   Vladimir Davydov   mm: vmscan: move ...
2873

0b06496a3   Johannes Weiner   mm: vmscan: rewor...
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
  			/*
  			 * If we already have plenty of memory free for
  			 * compaction in this zone, don't free any more.
  			 * Even though compaction is invoked for any
  			 * non-zero order, only frequent costly order
  			 * reclamation is disruptive enough to become a
  			 * noticeable problem, like transparent huge
  			 * page allocations.
  			 */
  			if (IS_ENABLED(CONFIG_COMPACTION) &&
  			    sc->order > PAGE_ALLOC_COSTLY_ORDER &&
4f588331b   Mel Gorman   mm, vmscan: avoid...
2885
  			    compaction_ready(zone, sc)) {
0b06496a3   Johannes Weiner   mm: vmscan: rewor...
2886
2887
  				sc->compaction_ready = true;
  				continue;
e0887c19b   Rik van Riel   vmscan: limit dir...
2888
  			}
0b06496a3   Johannes Weiner   mm: vmscan: rewor...
2889

0608f43da   Andrew Morton   revert "memcg, vm...
2890
  			/*
79dafcdca   Mel Gorman   mm, vmscan: by de...
2891
2892
2893
2894
2895
2896
2897
2898
2899
  			 * Shrink each node in the zonelist once. If the
  			 * zonelist is ordered by zone (not the default) then a
  			 * node may be shrunk multiple times but in that case
  			 * the user prefers lower zones being preserved.
  			 */
  			if (zone->zone_pgdat == last_pgdat)
  				continue;
  
  			/*
0608f43da   Andrew Morton   revert "memcg, vm...
2900
2901
2902
2903
2904
2905
  			 * This steals pages from memory cgroups over softlimit
  			 * and returns the number of reclaimed pages and
  			 * scanned pages. This works for global memory pressure
  			 * and balancing, not for a memcg's limit.
  			 */
  			nr_soft_scanned = 0;
ef8f23279   Mel Gorman   mm, memcg: move m...
2906
  			nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(zone->zone_pgdat,
0608f43da   Andrew Morton   revert "memcg, vm...
2907
2908
2909
2910
  						sc->order, sc->gfp_mask,
  						&nr_soft_scanned);
  			sc->nr_reclaimed += nr_soft_reclaimed;
  			sc->nr_scanned += nr_soft_scanned;
ac34a1a3c   KAMEZAWA Hiroyuki   memcg: fix direct...
2911
  			/* need some check for avoid more shrink_zone() */
1cfb419b3   KAMEZAWA Hiroyuki   per-zone and recl...
2912
  		}
408d85441   Nick Piggin   [PATCH] oom: use ...
2913

79dafcdca   Mel Gorman   mm, vmscan: by de...
2914
2915
2916
2917
  		/* See comment about same check for global reclaim above */
  		if (zone->zone_pgdat == last_pgdat)
  			continue;
  		last_pgdat = zone->zone_pgdat;
970a39a36   Mel Gorman   mm, vmscan: avoid...
2918
  		shrink_node(zone->zone_pgdat, sc);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2919
  	}
e0c23279c   Mel Gorman   vmscan: abort rec...
2920

65ec02cb9   Vladimir Davydov   mm: vmscan: move ...
2921
  	/*
619d0d76c   Weijie Yang   mm/vmscan: restor...
2922
2923
2924
2925
  	 * Restore to original mask to avoid the impact on the caller if we
  	 * promoted it to __GFP_HIGHMEM.
  	 */
  	sc->gfp_mask = orig_mask;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2926
  }
4f98a2fee   Rik van Riel   vmscan: split LRU...
2927

2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2928
2929
2930
2931
2932
2933
2934
2935
  static void snapshot_refaults(struct mem_cgroup *root_memcg, pg_data_t *pgdat)
  {
  	struct mem_cgroup *memcg;
  
  	memcg = mem_cgroup_iter(root_memcg, NULL, NULL);
  	do {
  		unsigned long refaults;
  		struct lruvec *lruvec;
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2936
  		lruvec = mem_cgroup_lruvec(pgdat, memcg);
205b20cc5   Johannes Weiner   mm: memcontrol: m...
2937
  		refaults = lruvec_page_state_local(lruvec, WORKINGSET_ACTIVATE);
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2938
2939
2940
  		lruvec->refaults = refaults;
  	} while ((memcg = mem_cgroup_iter(root_memcg, memcg, NULL)));
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2941
2942
2943
2944
2945
2946
2947
2948
  /*
   * This is the main entry point to direct page reclaim.
   *
   * If a full scan of the inactive list fails to free enough memory then we
   * are "out of memory" and something needs to be killed.
   *
   * If the caller is !__GFP_FS then the probability of a failure is reasonably
   * high - the zone may be full of dirty or under-writeback pages, which this
5b0830cb9   Jens Axboe   writeback: get ri...
2949
2950
2951
2952
   * caller can't do much about.  We kick the writeback threads and take explicit
   * naps in the hope that some of these pages can be written.  But if the
   * allocating task holds filesystem locks which prevent writeout this might not
   * work, and the allocation attempt will fail.
a41f24ea9   Nishanth Aravamudan   page allocator: s...
2953
2954
2955
   *
   * returns:	0, if no pages reclaimed
   * 		else, the number of pages reclaimed
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2956
   */
dac1d27bc   Mel Gorman   mm: use zonelists...
2957
  static unsigned long do_try_to_free_pages(struct zonelist *zonelist,
3115cd914   Vladimir Davydov   mm: vmscan: remov...
2958
  					  struct scan_control *sc)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2959
  {
241994ed8   Johannes Weiner   mm: memcontrol: d...
2960
  	int initial_priority = sc->priority;
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2961
2962
2963
  	pg_data_t *last_pgdat;
  	struct zoneref *z;
  	struct zone *zone;
241994ed8   Johannes Weiner   mm: memcontrol: d...
2964
  retry:
873b47717   Keika Kobayashi   per-task-delay-ac...
2965
  	delayacct_freepages_start();
89b5fae53   Johannes Weiner   mm: vmscan: disti...
2966
  	if (global_reclaim(sc))
7cc30fcfd   Mel Gorman   mm: vmstat: accou...
2967
  		__count_zid_vm_events(ALLOCSTALL, sc->reclaim_idx, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2968

9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
2969
  	do {
70ddf637e   Anton Vorontsov   memcg: add memory...
2970
2971
  		vmpressure_prio(sc->gfp_mask, sc->target_mem_cgroup,
  				sc->priority);
66e1707bc   Balbir Singh   Memory controller...
2972
  		sc->nr_scanned = 0;
0a0337e0d   Michal Hocko   mm, oom: rework o...
2973
  		shrink_zones(zonelist, sc);
c6a8a8c58   KOSAKI Motohiro   vmscan: recalcula...
2974

bb21c7ce1   KOSAKI Motohiro   vmscan: fix do_tr...
2975
  		if (sc->nr_reclaimed >= sc->nr_to_reclaim)
0b06496a3   Johannes Weiner   mm: vmscan: rewor...
2976
2977
2978
2979
  			break;
  
  		if (sc->compaction_ready)
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2980
2981
  
  		/*
0e50ce3b5   Minchan Kim   mm: use up free s...
2982
2983
2984
2985
2986
  		 * If we're getting trouble reclaiming, start doing
  		 * writepage even in laptop mode.
  		 */
  		if (sc->priority < DEF_PRIORITY - 2)
  			sc->may_writepage = 1;
0b06496a3   Johannes Weiner   mm: vmscan: rewor...
2987
  	} while (--sc->priority >= 0);
bb21c7ce1   KOSAKI Motohiro   vmscan: fix do_tr...
2988

2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2989
2990
2991
2992
2993
2994
2995
  	last_pgdat = NULL;
  	for_each_zone_zonelist_nodemask(zone, z, zonelist, sc->reclaim_idx,
  					sc->nodemask) {
  		if (zone->zone_pgdat == last_pgdat)
  			continue;
  		last_pgdat = zone->zone_pgdat;
  		snapshot_refaults(sc->target_mem_cgroup, zone->zone_pgdat);
e3c1ac586   Andrey Ryabinin   mm/vmscan: don't ...
2996
  		set_memcg_congestion(last_pgdat, sc->target_mem_cgroup, false);
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
2997
  	}
873b47717   Keika Kobayashi   per-task-delay-ac...
2998
  	delayacct_freepages_end();
bb21c7ce1   KOSAKI Motohiro   vmscan: fix do_tr...
2999
3000
  	if (sc->nr_reclaimed)
  		return sc->nr_reclaimed;
0cee34fd7   Mel Gorman   mm: vmscan: check...
3001
  	/* Aborted reclaim to try compaction? don't OOM, then */
0b06496a3   Johannes Weiner   mm: vmscan: rewor...
3002
  	if (sc->compaction_ready)
7335084d4   Mel Gorman   mm: vmscan: do no...
3003
  		return 1;
241994ed8   Johannes Weiner   mm: memcontrol: d...
3004
  	/* Untapped cgroup reserves?  Don't OOM, retry. */
d6622f636   Yisheng Xie   mm/vmscan: more r...
3005
  	if (sc->memcg_low_skipped) {
241994ed8   Johannes Weiner   mm: memcontrol: d...
3006
  		sc->priority = initial_priority;
d6622f636   Yisheng Xie   mm/vmscan: more r...
3007
3008
  		sc->memcg_low_reclaim = 1;
  		sc->memcg_low_skipped = 0;
241994ed8   Johannes Weiner   mm: memcontrol: d...
3009
3010
  		goto retry;
  	}
bb21c7ce1   KOSAKI Motohiro   vmscan: fix do_tr...
3011
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3012
  }
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3013
  static bool allow_direct_reclaim(pg_data_t *pgdat)
5515061d2   Mel Gorman   mm: throttle dire...
3014
3015
3016
3017
3018
3019
  {
  	struct zone *zone;
  	unsigned long pfmemalloc_reserve = 0;
  	unsigned long free_pages = 0;
  	int i;
  	bool wmark_ok;
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3020
3021
  	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
  		return true;
5515061d2   Mel Gorman   mm: throttle dire...
3022
3023
  	for (i = 0; i <= ZONE_NORMAL; i++) {
  		zone = &pgdat->node_zones[i];
d450abd81   Johannes Weiner   mm: fix check for...
3024
3025
3026
3027
  		if (!managed_zone(zone))
  			continue;
  
  		if (!zone_reclaimable_pages(zone))
675becce1   Mel Gorman   mm: vmscan: do no...
3028
  			continue;
5515061d2   Mel Gorman   mm: throttle dire...
3029
3030
3031
  		pfmemalloc_reserve += min_wmark_pages(zone);
  		free_pages += zone_page_state(zone, NR_FREE_PAGES);
  	}
675becce1   Mel Gorman   mm: vmscan: do no...
3032
3033
3034
  	/* If there are no reserves (unexpected config) then do not throttle */
  	if (!pfmemalloc_reserve)
  		return true;
5515061d2   Mel Gorman   mm: throttle dire...
3035
3036
3037
3038
  	wmark_ok = free_pages > pfmemalloc_reserve / 2;
  
  	/* kswapd must be awake if processes are being throttled */
  	if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) {
19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3039
3040
  		if (READ_ONCE(pgdat->kswapd_classzone_idx) > ZONE_NORMAL)
  			WRITE_ONCE(pgdat->kswapd_classzone_idx, ZONE_NORMAL);
5515061d2   Mel Gorman   mm: throttle dire...
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
  		wake_up_interruptible(&pgdat->kswapd_wait);
  	}
  
  	return wmark_ok;
  }
  
  /*
   * Throttle direct reclaimers if backing storage is backed by the network
   * and the PFMEMALLOC reserve for the preferred node is getting dangerously
   * depleted. kswapd will continue to make progress and wake the processes
50694c28f   Mel Gorman   mm: vmscan: check...
3051
3052
3053
3054
   * when the low watermark is reached.
   *
   * Returns true if a fatal signal was delivered during throttling. If this
   * happens, the page allocator should not consider triggering the OOM killer.
5515061d2   Mel Gorman   mm: throttle dire...
3055
   */
50694c28f   Mel Gorman   mm: vmscan: check...
3056
  static bool throttle_direct_reclaim(gfp_t gfp_mask, struct zonelist *zonelist,
5515061d2   Mel Gorman   mm: throttle dire...
3057
3058
  					nodemask_t *nodemask)
  {
675becce1   Mel Gorman   mm: vmscan: do no...
3059
  	struct zoneref *z;
5515061d2   Mel Gorman   mm: throttle dire...
3060
  	struct zone *zone;
675becce1   Mel Gorman   mm: vmscan: do no...
3061
  	pg_data_t *pgdat = NULL;
5515061d2   Mel Gorman   mm: throttle dire...
3062
3063
3064
3065
3066
3067
3068
3069
3070
  
  	/*
  	 * Kernel threads should not be throttled as they may be indirectly
  	 * responsible for cleaning pages necessary for reclaim to make forward
  	 * progress. kjournald for example may enter direct reclaim while
  	 * committing a transaction where throttling it could forcing other
  	 * processes to block on log_wait_commit().
  	 */
  	if (current->flags & PF_KTHREAD)
50694c28f   Mel Gorman   mm: vmscan: check...
3071
3072
3073
3074
3075
3076
3077
3078
  		goto out;
  
  	/*
  	 * If a fatal signal is pending, this process should not throttle.
  	 * It should return quickly so it can exit and free its memory
  	 */
  	if (fatal_signal_pending(current))
  		goto out;
5515061d2   Mel Gorman   mm: throttle dire...
3079

675becce1   Mel Gorman   mm: vmscan: do no...
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
  	/*
  	 * Check if the pfmemalloc reserves are ok by finding the first node
  	 * with a usable ZONE_NORMAL or lower zone. The expectation is that
  	 * GFP_KERNEL will be required for allocating network buffers when
  	 * swapping over the network so ZONE_HIGHMEM is unusable.
  	 *
  	 * Throttling is based on the first usable node and throttled processes
  	 * wait on a queue until kswapd makes progress and wakes them. There
  	 * is an affinity then between processes waking up and where reclaim
  	 * progress has been made assuming the process wakes on the same node.
  	 * More importantly, processes running on remote nodes will not compete
  	 * for remote pfmemalloc reserves and processes on different nodes
  	 * should make reasonable progress.
  	 */
  	for_each_zone_zonelist_nodemask(zone, z, zonelist,
17636faad   Michael S. Tsirkin   mm/vmscan: fix hi...
3095
  					gfp_zone(gfp_mask), nodemask) {
675becce1   Mel Gorman   mm: vmscan: do no...
3096
3097
3098
3099
3100
  		if (zone_idx(zone) > ZONE_NORMAL)
  			continue;
  
  		/* Throttle based on the first usable node */
  		pgdat = zone->zone_pgdat;
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3101
  		if (allow_direct_reclaim(pgdat))
675becce1   Mel Gorman   mm: vmscan: do no...
3102
3103
3104
3105
3106
3107
  			goto out;
  		break;
  	}
  
  	/* If no zone was usable by the allocation flags then do not throttle */
  	if (!pgdat)
50694c28f   Mel Gorman   mm: vmscan: check...
3108
  		goto out;
5515061d2   Mel Gorman   mm: throttle dire...
3109

68243e76e   Mel Gorman   mm: account for t...
3110
3111
  	/* Account for the throttling */
  	count_vm_event(PGSCAN_DIRECT_THROTTLE);
5515061d2   Mel Gorman   mm: throttle dire...
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
  	/*
  	 * If the caller cannot enter the filesystem, it's possible that it
  	 * is due to the caller holding an FS lock or performing a journal
  	 * transaction in the case of a filesystem like ext[3|4]. In this case,
  	 * it is not safe to block on pfmemalloc_wait as kswapd could be
  	 * blocked waiting on the same lock. Instead, throttle for up to a
  	 * second before continuing.
  	 */
  	if (!(gfp_mask & __GFP_FS)) {
  		wait_event_interruptible_timeout(pgdat->pfmemalloc_wait,
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3122
  			allow_direct_reclaim(pgdat), HZ);
50694c28f   Mel Gorman   mm: vmscan: check...
3123
3124
  
  		goto check_pending;
5515061d2   Mel Gorman   mm: throttle dire...
3125
3126
3127
3128
  	}
  
  	/* Throttle until kswapd wakes the process */
  	wait_event_killable(zone->zone_pgdat->pfmemalloc_wait,
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3129
  		allow_direct_reclaim(pgdat));
50694c28f   Mel Gorman   mm: vmscan: check...
3130
3131
3132
3133
3134
3135
3136
  
  check_pending:
  	if (fatal_signal_pending(current))
  		return true;
  
  out:
  	return false;
5515061d2   Mel Gorman   mm: throttle dire...
3137
  }
dac1d27bc   Mel Gorman   mm: use zonelists...
3138
  unsigned long try_to_free_pages(struct zonelist *zonelist, int order,
327c0e968   KAMEZAWA Hiroyuki   vmscan: fix it to...
3139
  				gfp_t gfp_mask, nodemask_t *nodemask)
66e1707bc   Balbir Singh   Memory controller...
3140
  {
33906bc5c   Mel Gorman   vmscan: tracing: ...
3141
  	unsigned long nr_reclaimed;
66e1707bc   Balbir Singh   Memory controller...
3142
  	struct scan_control sc = {
ee814fe23   Johannes Weiner   mm: vmscan: clean...
3143
  		.nr_to_reclaim = SWAP_CLUSTER_MAX,
f2f43e566   Nick Desaulniers   mm/vmscan.c: fix ...
3144
  		.gfp_mask = current_gfp_context(gfp_mask),
b2e18757f   Mel Gorman   mm, vmscan: begin...
3145
  		.reclaim_idx = gfp_zone(gfp_mask),
ee814fe23   Johannes Weiner   mm: vmscan: clean...
3146
3147
3148
  		.order = order,
  		.nodemask = nodemask,
  		.priority = DEF_PRIORITY,
66e1707bc   Balbir Singh   Memory controller...
3149
  		.may_writepage = !laptop_mode,
a6dc60f89   Johannes Weiner   vmscan: rename sc...
3150
  		.may_unmap = 1,
2e2e42598   KOSAKI Motohiro   vmscan,memcg: rei...
3151
  		.may_swap = 1,
66e1707bc   Balbir Singh   Memory controller...
3152
  	};
5515061d2   Mel Gorman   mm: throttle dire...
3153
  	/*
bb451fdf3   Greg Thelen   mm/vmscan.c: cond...
3154
3155
3156
3157
3158
3159
3160
3161
  	 * scan_control uses s8 fields for order, priority, and reclaim_idx.
  	 * Confirm they are large enough for max values.
  	 */
  	BUILD_BUG_ON(MAX_ORDER > S8_MAX);
  	BUILD_BUG_ON(DEF_PRIORITY > S8_MAX);
  	BUILD_BUG_ON(MAX_NR_ZONES > S8_MAX);
  
  	/*
50694c28f   Mel Gorman   mm: vmscan: check...
3162
3163
3164
  	 * Do not enter reclaim if fatal signal was delivered while throttled.
  	 * 1 is returned so that the page allocator does not OOM kill at this
  	 * point.
5515061d2   Mel Gorman   mm: throttle dire...
3165
  	 */
f2f43e566   Nick Desaulniers   mm/vmscan.c: fix ...
3166
  	if (throttle_direct_reclaim(sc.gfp_mask, zonelist, nodemask))
5515061d2   Mel Gorman   mm: throttle dire...
3167
  		return 1;
1732d2b01   Andrew Morton   mm/vmscan.c: add ...
3168
  	set_task_reclaim_state(current, &sc.reclaim_state);
3481c37ff   Yafang Shao   mm/vmscan: drop m...
3169
  	trace_mm_vmscan_direct_reclaim_begin(order, sc.gfp_mask);
33906bc5c   Mel Gorman   vmscan: tracing: ...
3170

3115cd914   Vladimir Davydov   mm: vmscan: remov...
3171
  	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
33906bc5c   Mel Gorman   vmscan: tracing: ...
3172
3173
  
  	trace_mm_vmscan_direct_reclaim_end(nr_reclaimed);
1732d2b01   Andrew Morton   mm/vmscan.c: add ...
3174
  	set_task_reclaim_state(current, NULL);
33906bc5c   Mel Gorman   vmscan: tracing: ...
3175
3176
  
  	return nr_reclaimed;
66e1707bc   Balbir Singh   Memory controller...
3177
  }
c255a4580   Andrew Morton   memcg: rename con...
3178
  #ifdef CONFIG_MEMCG
66e1707bc   Balbir Singh   Memory controller...
3179

d2e5fb927   Michal Hocko   mm, memcg: do not...
3180
  /* Only used by soft limit reclaim. Do not reuse for anything else. */
a9dd0a831   Mel Gorman   mm, vmscan: make ...
3181
  unsigned long mem_cgroup_shrink_node(struct mem_cgroup *memcg,
4e4169535   Balbir Singh   memory controller...
3182
  						gfp_t gfp_mask, bool noswap,
ef8f23279   Mel Gorman   mm, memcg: move m...
3183
  						pg_data_t *pgdat,
0ae5e89c6   Ying Han   memcg: count the ...
3184
  						unsigned long *nr_scanned)
4e4169535   Balbir Singh   memory controller...
3185
3186
  {
  	struct scan_control sc = {
b8f5c5664   KOSAKI Motohiro   memcg: sc.nr_to_r...
3187
  		.nr_to_reclaim = SWAP_CLUSTER_MAX,
ee814fe23   Johannes Weiner   mm: vmscan: clean...
3188
  		.target_mem_cgroup = memcg,
4e4169535   Balbir Singh   memory controller...
3189
3190
  		.may_writepage = !laptop_mode,
  		.may_unmap = 1,
b2e18757f   Mel Gorman   mm, vmscan: begin...
3191
  		.reclaim_idx = MAX_NR_ZONES - 1,
4e4169535   Balbir Singh   memory controller...
3192
  		.may_swap = !noswap,
4e4169535   Balbir Singh   memory controller...
3193
  	};
6b4f7799c   Johannes Weiner   mm: vmscan: invok...
3194
  	unsigned long lru_pages;
0ae5e89c6   Ying Han   memcg: count the ...
3195

d2e5fb927   Michal Hocko   mm, memcg: do not...
3196
  	WARN_ON_ONCE(!current->reclaim_state);
4e4169535   Balbir Singh   memory controller...
3197
3198
  	sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) |
  			(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK);
bdce6d9eb   KOSAKI Motohiro   memcg, vmscan: ad...
3199

9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
3200
  	trace_mm_vmscan_memcg_softlimit_reclaim_begin(sc.order,
3481c37ff   Yafang Shao   mm/vmscan: drop m...
3201
  						      sc.gfp_mask);
bdce6d9eb   KOSAKI Motohiro   memcg, vmscan: ad...
3202

4e4169535   Balbir Singh   memory controller...
3203
3204
3205
  	/*
  	 * NOTE: Although we can get the priority field, using it
  	 * here is not a good idea, since it limits the pages we can scan.
a9dd0a831   Mel Gorman   mm, vmscan: make ...
3206
  	 * if we don't reclaim here, the shrink_node from balance_pgdat
4e4169535   Balbir Singh   memory controller...
3207
3208
3209
  	 * will pick up pages from other mem cgroup's as well. We hack
  	 * the priority and make it zero.
  	 */
ef8f23279   Mel Gorman   mm, memcg: move m...
3210
  	shrink_node_memcg(pgdat, memcg, &sc, &lru_pages);
bdce6d9eb   KOSAKI Motohiro   memcg, vmscan: ad...
3211
3212
  
  	trace_mm_vmscan_memcg_softlimit_reclaim_end(sc.nr_reclaimed);
0ae5e89c6   Ying Han   memcg: count the ...
3213
  	*nr_scanned = sc.nr_scanned;
0308f7cf1   Yafang Shao   mm/vmscan.c: calc...
3214

4e4169535   Balbir Singh   memory controller...
3215
3216
  	return sc.nr_reclaimed;
  }
72835c86c   Johannes Weiner   mm: unify remaini...
3217
  unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *memcg,
b70a2a21d   Johannes Weiner   mm: memcontrol: f...
3218
  					   unsigned long nr_pages,
a7885eb8a   KOSAKI Motohiro   memcg: swappiness
3219
  					   gfp_t gfp_mask,
b70a2a21d   Johannes Weiner   mm: memcontrol: f...
3220
  					   bool may_swap)
66e1707bc   Balbir Singh   Memory controller...
3221
  {
4e4169535   Balbir Singh   memory controller...
3222
  	struct zonelist *zonelist;
bdce6d9eb   KOSAKI Motohiro   memcg, vmscan: ad...
3223
  	unsigned long nr_reclaimed;
eb414681d   Johannes Weiner   psi: pressure sta...
3224
  	unsigned long pflags;
889976dbc   Ying Han   memcg: reclaim me...
3225
  	int nid;
499118e96   Vlastimil Babka   mm: introduce mem...
3226
  	unsigned int noreclaim_flag;
66e1707bc   Balbir Singh   Memory controller...
3227
  	struct scan_control sc = {
b70a2a21d   Johannes Weiner   mm: memcontrol: f...
3228
  		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
7dea19f9e   Michal Hocko   mm: introduce mem...
3229
  		.gfp_mask = (current_gfp_context(gfp_mask) & GFP_RECLAIM_MASK) |
a09ed5e00   Ying Han   vmscan: change sh...
3230
  				(GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK),
b2e18757f   Mel Gorman   mm, vmscan: begin...
3231
  		.reclaim_idx = MAX_NR_ZONES - 1,
ee814fe23   Johannes Weiner   mm: vmscan: clean...
3232
3233
3234
3235
  		.target_mem_cgroup = memcg,
  		.priority = DEF_PRIORITY,
  		.may_writepage = !laptop_mode,
  		.may_unmap = 1,
b70a2a21d   Johannes Weiner   mm: memcontrol: f...
3236
  		.may_swap = may_swap,
a09ed5e00   Ying Han   vmscan: change sh...
3237
  	};
66e1707bc   Balbir Singh   Memory controller...
3238

1732d2b01   Andrew Morton   mm/vmscan.c: add ...
3239
  	set_task_reclaim_state(current, &sc.reclaim_state);
889976dbc   Ying Han   memcg: reclaim me...
3240
3241
3242
3243
3244
  	/*
  	 * Unlike direct reclaim via alloc_pages(), memcg's reclaim doesn't
  	 * take care of from where we get pages. So the node where we start the
  	 * scan does not need to be the current node.
  	 */
72835c86c   Johannes Weiner   mm: unify remaini...
3245
  	nid = mem_cgroup_select_victim_node(memcg);
889976dbc   Ying Han   memcg: reclaim me...
3246

c9634cf01   Aneesh Kumar K.V   mm: use zonelist ...
3247
  	zonelist = &NODE_DATA(nid)->node_zonelists[ZONELIST_FALLBACK];
bdce6d9eb   KOSAKI Motohiro   memcg, vmscan: ad...
3248

3481c37ff   Yafang Shao   mm/vmscan: drop m...
3249
  	trace_mm_vmscan_memcg_reclaim_begin(0, sc.gfp_mask);
bdce6d9eb   KOSAKI Motohiro   memcg, vmscan: ad...
3250

eb414681d   Johannes Weiner   psi: pressure sta...
3251
  	psi_memstall_enter(&pflags);
499118e96   Vlastimil Babka   mm: introduce mem...
3252
  	noreclaim_flag = memalloc_noreclaim_save();
eb414681d   Johannes Weiner   psi: pressure sta...
3253

3115cd914   Vladimir Davydov   mm: vmscan: remov...
3254
  	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
eb414681d   Johannes Weiner   psi: pressure sta...
3255

499118e96   Vlastimil Babka   mm: introduce mem...
3256
  	memalloc_noreclaim_restore(noreclaim_flag);
eb414681d   Johannes Weiner   psi: pressure sta...
3257
  	psi_memstall_leave(&pflags);
bdce6d9eb   KOSAKI Motohiro   memcg, vmscan: ad...
3258
3259
  
  	trace_mm_vmscan_memcg_reclaim_end(nr_reclaimed);
1732d2b01   Andrew Morton   mm/vmscan.c: add ...
3260
  	set_task_reclaim_state(current, NULL);
bdce6d9eb   KOSAKI Motohiro   memcg, vmscan: ad...
3261
3262
  
  	return nr_reclaimed;
66e1707bc   Balbir Singh   Memory controller...
3263
3264
  }
  #endif
1d82de618   Mel Gorman   mm, vmscan: make ...
3265
  static void age_active_anon(struct pglist_data *pgdat,
ef8f23279   Mel Gorman   mm, memcg: move m...
3266
  				struct scan_control *sc)
f16015fbf   Johannes Weiner   mm: vmscan: disti...
3267
  {
b95a2f2d4   Johannes Weiner   mm: vmscan: conve...
3268
  	struct mem_cgroup *memcg;
f16015fbf   Johannes Weiner   mm: vmscan: disti...
3269

b95a2f2d4   Johannes Weiner   mm: vmscan: conve...
3270
3271
3272
3273
3274
  	if (!total_swap_pages)
  		return;
  
  	memcg = mem_cgroup_iter(NULL, NULL, NULL);
  	do {
ef8f23279   Mel Gorman   mm, memcg: move m...
3275
  		struct lruvec *lruvec = mem_cgroup_lruvec(pgdat, memcg);
b95a2f2d4   Johannes Weiner   mm: vmscan: conve...
3276

3b991208b   Johannes Weiner   mm: fix inactive ...
3277
  		if (inactive_list_is_low(lruvec, false, sc, true))
1a93be0e7   Konstantin Khlebnikov   mm/vmscan: push l...
3278
  			shrink_active_list(SWAP_CLUSTER_MAX, lruvec,
9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
3279
  					   sc, LRU_ACTIVE_ANON);
b95a2f2d4   Johannes Weiner   mm: vmscan: conve...
3280
3281
3282
  
  		memcg = mem_cgroup_iter(NULL, memcg, NULL);
  	} while (memcg);
f16015fbf   Johannes Weiner   mm: vmscan: disti...
3283
  }
1c30844d2   Mel Gorman   mm: reclaim small...
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
  static bool pgdat_watermark_boosted(pg_data_t *pgdat, int classzone_idx)
  {
  	int i;
  	struct zone *zone;
  
  	/*
  	 * Check for watermark boosts top-down as the higher zones
  	 * are more likely to be boosted. Both watermarks and boosts
  	 * should not be checked at the time time as reclaim would
  	 * start prematurely when there is no boosting and a lower
  	 * zone is balanced.
  	 */
  	for (i = classzone_idx; i >= 0; i--) {
  		zone = pgdat->node_zones + i;
  		if (!managed_zone(zone))
  			continue;
  
  		if (zone->watermark_boost)
  			return true;
  	}
  
  	return false;
  }
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3307
3308
3309
3310
3311
  /*
   * Returns true if there is an eligible zone balanced for the request order
   * and classzone_idx
   */
  static bool pgdat_balanced(pg_data_t *pgdat, int order, int classzone_idx)
60cefed48   Johannes Weiner   mm: vmscan: fix e...
3312
  {
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3313
3314
3315
  	int i;
  	unsigned long mark = -1;
  	struct zone *zone;
60cefed48   Johannes Weiner   mm: vmscan: fix e...
3316

1c30844d2   Mel Gorman   mm: reclaim small...
3317
3318
3319
3320
  	/*
  	 * Check watermarks bottom-up as lower zones are more likely to
  	 * meet watermarks.
  	 */
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3321
3322
  	for (i = 0; i <= classzone_idx; i++) {
  		zone = pgdat->node_zones + i;
6256c6b49   Mel Gorman   mm, vmscan: remov...
3323

e716f2eb2   Mel Gorman   mm, vmscan: preve...
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
  		if (!managed_zone(zone))
  			continue;
  
  		mark = high_wmark_pages(zone);
  		if (zone_watermark_ok_safe(zone, order, mark, classzone_idx))
  			return true;
  	}
  
  	/*
  	 * If a node has no populated zone within classzone_idx, it does not
  	 * need balancing by definition. This can happen if a zone-restricted
  	 * allocation tries to wake a remote kswapd.
  	 */
  	if (mark == -1)
  		return true;
  
  	return false;
60cefed48   Johannes Weiner   mm: vmscan: fix e...
3341
  }
631b6e083   Mel Gorman   mm, vmscan: only ...
3342
3343
3344
3345
3346
3347
3348
  /* Clear pgdat state for congested, dirty or under writeback. */
  static void clear_pgdat_congested(pg_data_t *pgdat)
  {
  	clear_bit(PGDAT_CONGESTED, &pgdat->flags);
  	clear_bit(PGDAT_DIRTY, &pgdat->flags);
  	clear_bit(PGDAT_WRITEBACK, &pgdat->flags);
  }
1741c8775   Mel Gorman   mm: kswapd: keep ...
3349
  /*
5515061d2   Mel Gorman   mm: throttle dire...
3350
3351
3352
3353
3354
   * Prepare kswapd for sleeping. This verifies that there are no processes
   * waiting in throttle_direct_reclaim() and that watermarks have been met.
   *
   * Returns true if kswapd is ready to sleep
   */
d9f21d426   Mel Gorman   mm, vmscan: avoid...
3355
  static bool prepare_kswapd_sleep(pg_data_t *pgdat, int order, int classzone_idx)
f50de2d38   Mel Gorman   vmscan: have kswa...
3356
  {
5515061d2   Mel Gorman   mm: throttle dire...
3357
  	/*
9e5e36617   Vlastimil Babka   mm, vmscan: preve...
3358
  	 * The throttled processes are normally woken up in balance_pgdat() as
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3359
  	 * soon as allow_direct_reclaim() is true. But there is a potential
9e5e36617   Vlastimil Babka   mm, vmscan: preve...
3360
3361
3362
3363
3364
3365
3366
3367
3368
  	 * race between when kswapd checks the watermarks and a process gets
  	 * throttled. There is also a potential race if processes get
  	 * throttled, kswapd wakes, a large process exits thereby balancing the
  	 * zones, which causes kswapd to exit balance_pgdat() before reaching
  	 * the wake up checks. If kswapd is going to sleep, no process should
  	 * be sleeping on pfmemalloc_wait, so wake them now if necessary. If
  	 * the wake up is premature, processes will wake kswapd and get
  	 * throttled again. The difference from wake ups in balance_pgdat() is
  	 * that here we are under prepare_to_wait().
5515061d2   Mel Gorman   mm: throttle dire...
3369
  	 */
9e5e36617   Vlastimil Babka   mm, vmscan: preve...
3370
3371
  	if (waitqueue_active(&pgdat->pfmemalloc_wait))
  		wake_up_all(&pgdat->pfmemalloc_wait);
f50de2d38   Mel Gorman   vmscan: have kswa...
3372

c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3373
3374
3375
  	/* Hopeless node, leave it to direct reclaim */
  	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES)
  		return true;
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3376
3377
3378
  	if (pgdat_balanced(pgdat, order, classzone_idx)) {
  		clear_pgdat_congested(pgdat);
  		return true;
1d82de618   Mel Gorman   mm, vmscan: make ...
3379
  	}
333b0a459   Shantanu Goel   mm, vmscan: fix z...
3380
  	return false;
f50de2d38   Mel Gorman   vmscan: have kswa...
3381
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3382
  /*
1d82de618   Mel Gorman   mm, vmscan: make ...
3383
3384
   * kswapd shrinks a node of pages that are at or below the highest usable
   * zone that is currently unbalanced.
b8e83b942   Mel Gorman   mm: vmscan: flatt...
3385
3386
   *
   * Returns true if kswapd scanned at least the requested number of pages to
283aba9f9   Mel Gorman   mm: vmscan: block...
3387
3388
   * reclaim or if the lack of progress was due to pages under writeback.
   * This is used to determine if the scanning priority needs to be raised.
75485363c   Mel Gorman   mm: vmscan: limit...
3389
   */
1d82de618   Mel Gorman   mm, vmscan: make ...
3390
  static bool kswapd_shrink_node(pg_data_t *pgdat,
accf62422   Vlastimil Babka   mm, kswapd: repla...
3391
  			       struct scan_control *sc)
75485363c   Mel Gorman   mm: vmscan: limit...
3392
  {
1d82de618   Mel Gorman   mm, vmscan: make ...
3393
3394
  	struct zone *zone;
  	int z;
75485363c   Mel Gorman   mm: vmscan: limit...
3395

1d82de618   Mel Gorman   mm, vmscan: make ...
3396
3397
  	/* Reclaim a number of pages proportional to the number of zones */
  	sc->nr_to_reclaim = 0;
970a39a36   Mel Gorman   mm, vmscan: avoid...
3398
  	for (z = 0; z <= sc->reclaim_idx; z++) {
1d82de618   Mel Gorman   mm, vmscan: make ...
3399
  		zone = pgdat->node_zones + z;
6aa303def   Mel Gorman   mm, vmscan: only ...
3400
  		if (!managed_zone(zone))
1d82de618   Mel Gorman   mm, vmscan: make ...
3401
  			continue;
7c954f6de   Mel Gorman   mm: vmscan: move ...
3402

1d82de618   Mel Gorman   mm, vmscan: make ...
3403
3404
  		sc->nr_to_reclaim += max(high_wmark_pages(zone), SWAP_CLUSTER_MAX);
  	}
7c954f6de   Mel Gorman   mm: vmscan: move ...
3405
3406
  
  	/*
1d82de618   Mel Gorman   mm, vmscan: make ...
3407
3408
  	 * Historically care was taken to put equal pressure on all zones but
  	 * now pressure is applied based on node LRU order.
7c954f6de   Mel Gorman   mm: vmscan: move ...
3409
  	 */
970a39a36   Mel Gorman   mm, vmscan: avoid...
3410
  	shrink_node(pgdat, sc);
283aba9f9   Mel Gorman   mm: vmscan: block...
3411

7c954f6de   Mel Gorman   mm: vmscan: move ...
3412
  	/*
1d82de618   Mel Gorman   mm, vmscan: make ...
3413
3414
3415
3416
3417
  	 * Fragmentation may mean that the system cannot be rebalanced for
  	 * high-order allocations. If twice the allocation size has been
  	 * reclaimed then recheck watermarks only at order-0 to prevent
  	 * excessive reclaim. Assume that a process requested a high-order
  	 * can direct reclaim/compact.
7c954f6de   Mel Gorman   mm: vmscan: move ...
3418
  	 */
9861a62c3   Vlastimil Babka   mm, compaction: c...
3419
  	if (sc->order && sc->nr_reclaimed >= compact_gap(sc->order))
1d82de618   Mel Gorman   mm, vmscan: make ...
3420
  		sc->order = 0;
7c954f6de   Mel Gorman   mm: vmscan: move ...
3421

b8e83b942   Mel Gorman   mm: vmscan: flatt...
3422
  	return sc->nr_scanned >= sc->nr_to_reclaim;
75485363c   Mel Gorman   mm: vmscan: limit...
3423
3424
3425
  }
  
  /*
1d82de618   Mel Gorman   mm, vmscan: make ...
3426
3427
3428
   * For kswapd, balance_pgdat() will reclaim pages across a node from zones
   * that are eligible for use by the caller until at least one zone is
   * balanced.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3429
   *
1d82de618   Mel Gorman   mm, vmscan: make ...
3430
   * Returns the order kswapd finished reclaiming at.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3431
3432
   *
   * kswapd scans the zones in the highmem->normal->dma direction.  It skips
418589663   Mel Gorman   page allocator: u...
3433
   * zones which have free_pages > high_wmark_pages(zone), but once a zone is
8bb4e7a2e   Wei Yang   mm: fix some typo...
3434
   * found to have free_pages <= high_wmark_pages(zone), any page in that zone
1d82de618   Mel Gorman   mm, vmscan: make ...
3435
3436
   * or lower is eligible for reclaim until at least one usable zone is
   * balanced.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3437
   */
accf62422   Vlastimil Babka   mm, kswapd: repla...
3438
  static int balance_pgdat(pg_data_t *pgdat, int order, int classzone_idx)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3439
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3440
  	int i;
0608f43da   Andrew Morton   revert "memcg, vm...
3441
3442
  	unsigned long nr_soft_reclaimed;
  	unsigned long nr_soft_scanned;
eb414681d   Johannes Weiner   psi: pressure sta...
3443
  	unsigned long pflags;
1c30844d2   Mel Gorman   mm: reclaim small...
3444
3445
3446
  	unsigned long nr_boost_reclaim;
  	unsigned long zone_boosts[MAX_NR_ZONES] = { 0, };
  	bool boosted;
1d82de618   Mel Gorman   mm, vmscan: make ...
3447
  	struct zone *zone;
179e96395   Andrew Morton   [PATCH] vmscan: s...
3448
3449
  	struct scan_control sc = {
  		.gfp_mask = GFP_KERNEL,
ee814fe23   Johannes Weiner   mm: vmscan: clean...
3450
  		.order = order,
a6dc60f89   Johannes Weiner   vmscan: rename sc...
3451
  		.may_unmap = 1,
179e96395   Andrew Morton   [PATCH] vmscan: s...
3452
  	};
93781325d   Omar Sandoval   lockdep: fix fs_r...
3453

1732d2b01   Andrew Morton   mm/vmscan.c: add ...
3454
  	set_task_reclaim_state(current, &sc.reclaim_state);
eb414681d   Johannes Weiner   psi: pressure sta...
3455
  	psi_memstall_enter(&pflags);
93781325d   Omar Sandoval   lockdep: fix fs_r...
3456
  	__fs_reclaim_acquire();
f8891e5e1   Christoph Lameter   [PATCH] Light wei...
3457
  	count_vm_event(PAGEOUTRUN);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3458

1c30844d2   Mel Gorman   mm: reclaim small...
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
  	/*
  	 * Account for the reclaim boost. Note that the zone boost is left in
  	 * place so that parallel allocations that are near the watermark will
  	 * stall or direct reclaim until kswapd is finished.
  	 */
  	nr_boost_reclaim = 0;
  	for (i = 0; i <= classzone_idx; i++) {
  		zone = pgdat->node_zones + i;
  		if (!managed_zone(zone))
  			continue;
  
  		nr_boost_reclaim += zone->watermark_boost;
  		zone_boosts[i] = zone->watermark_boost;
  	}
  	boosted = nr_boost_reclaim;
  
  restart:
  	sc.priority = DEF_PRIORITY;
9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
3477
  	do {
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3478
  		unsigned long nr_reclaimed = sc.nr_reclaimed;
b8e83b942   Mel Gorman   mm: vmscan: flatt...
3479
  		bool raise_priority = true;
1c30844d2   Mel Gorman   mm: reclaim small...
3480
  		bool balanced;
93781325d   Omar Sandoval   lockdep: fix fs_r...
3481
  		bool ret;
b8e83b942   Mel Gorman   mm: vmscan: flatt...
3482

84c7a7771   Mel Gorman   mm, vmscan: Have ...
3483
  		sc.reclaim_idx = classzone_idx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3484

86c79f6b5   Mel Gorman   mm: vmscan: do no...
3485
  		/*
84c7a7771   Mel Gorman   mm, vmscan: Have ...
3486
3487
3488
3489
3490
3491
3492
3493
  		 * If the number of buffer_heads exceeds the maximum allowed
  		 * then consider reclaiming from all zones. This has a dual
  		 * purpose -- on 64-bit systems it is expected that
  		 * buffer_heads are stripped during active rotation. On 32-bit
  		 * systems, highmem pages can pin lowmem memory and shrinking
  		 * buffers can relieve lowmem pressure. Reclaim may still not
  		 * go ahead if all eligible zones for the original allocation
  		 * request are balanced to avoid excessive reclaim from kswapd.
86c79f6b5   Mel Gorman   mm: vmscan: do no...
3494
3495
3496
3497
  		 */
  		if (buffer_heads_over_limit) {
  			for (i = MAX_NR_ZONES - 1; i >= 0; i--) {
  				zone = pgdat->node_zones + i;
6aa303def   Mel Gorman   mm, vmscan: only ...
3498
  				if (!managed_zone(zone))
86c79f6b5   Mel Gorman   mm: vmscan: do no...
3499
  					continue;
cc715d99e   Mel Gorman   mm: vmscan: forci...
3500

970a39a36   Mel Gorman   mm, vmscan: avoid...
3501
  				sc.reclaim_idx = i;
e1dbeda60   Andrew Morton   [PATCH] balance_p...
3502
  				break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3503
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3504
  		}
dafcb73e3   Zlatko Calusic   mm: avoid calling...
3505

86c79f6b5   Mel Gorman   mm: vmscan: do no...
3506
  		/*
1c30844d2   Mel Gorman   mm: reclaim small...
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
  		 * If the pgdat is imbalanced then ignore boosting and preserve
  		 * the watermarks for a later time and restart. Note that the
  		 * zone watermarks will be still reset at the end of balancing
  		 * on the grounds that the normal reclaim should be enough to
  		 * re-evaluate if boosting is required when kswapd next wakes.
  		 */
  		balanced = pgdat_balanced(pgdat, sc.order, classzone_idx);
  		if (!balanced && nr_boost_reclaim) {
  			nr_boost_reclaim = 0;
  			goto restart;
  		}
  
  		/*
  		 * If boosting is not active then only reclaim if there are no
  		 * eligible zones. Note that sc.reclaim_idx is not used as
  		 * buffer_heads_over_limit may have adjusted it.
86c79f6b5   Mel Gorman   mm: vmscan: do no...
3523
  		 */
1c30844d2   Mel Gorman   mm: reclaim small...
3524
  		if (!nr_boost_reclaim && balanced)
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3525
  			goto out;
e1dbeda60   Andrew Morton   [PATCH] balance_p...
3526

1c30844d2   Mel Gorman   mm: reclaim small...
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
  		/* Limit the priority of boosting to avoid reclaim writeback */
  		if (nr_boost_reclaim && sc.priority == DEF_PRIORITY - 2)
  			raise_priority = false;
  
  		/*
  		 * Do not writeback or swap pages for boosted reclaim. The
  		 * intent is to relieve pressure not issue sub-optimal IO
  		 * from reclaim context. If no pages are reclaimed, the
  		 * reclaim will be aborted.
  		 */
  		sc.may_writepage = !laptop_mode && !nr_boost_reclaim;
  		sc.may_swap = !nr_boost_reclaim;
1c30844d2   Mel Gorman   mm: reclaim small...
3539

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3540
  		/*
1d82de618   Mel Gorman   mm, vmscan: make ...
3541
3542
3543
3544
3545
  		 * Do some background aging of the anon list, to give
  		 * pages a chance to be referenced before reclaiming. All
  		 * pages are rotated regardless of classzone as this is
  		 * about consistent aging.
  		 */
ef8f23279   Mel Gorman   mm, memcg: move m...
3546
  		age_active_anon(pgdat, &sc);
1d82de618   Mel Gorman   mm, vmscan: make ...
3547
3548
  
  		/*
b7ea3c417   Mel Gorman   mm: vmscan: check...
3549
3550
3551
  		 * If we're getting trouble reclaiming, start doing writepage
  		 * even in laptop mode.
  		 */
047d72c30   Johannes Weiner   mm: remove seemin...
3552
  		if (sc.priority < DEF_PRIORITY - 2)
b7ea3c417   Mel Gorman   mm: vmscan: check...
3553
  			sc.may_writepage = 1;
1d82de618   Mel Gorman   mm, vmscan: make ...
3554
3555
3556
  		/* Call soft limit reclaim before calling shrink_node. */
  		sc.nr_scanned = 0;
  		nr_soft_scanned = 0;
ef8f23279   Mel Gorman   mm, memcg: move m...
3557
  		nr_soft_reclaimed = mem_cgroup_soft_limit_reclaim(pgdat, sc.order,
1d82de618   Mel Gorman   mm, vmscan: make ...
3558
3559
  						sc.gfp_mask, &nr_soft_scanned);
  		sc.nr_reclaimed += nr_soft_reclaimed;
b7ea3c417   Mel Gorman   mm: vmscan: check...
3560
  		/*
1d82de618   Mel Gorman   mm, vmscan: make ...
3561
3562
3563
  		 * There should be no need to raise the scanning priority if
  		 * enough pages are already being scanned that that high
  		 * watermark would be met at 100% efficiency.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3564
  		 */
970a39a36   Mel Gorman   mm, vmscan: avoid...
3565
  		if (kswapd_shrink_node(pgdat, &sc))
1d82de618   Mel Gorman   mm, vmscan: make ...
3566
  			raise_priority = false;
5515061d2   Mel Gorman   mm: throttle dire...
3567
3568
3569
3570
3571
3572
3573
  
  		/*
  		 * If the low watermark is met there is no need for processes
  		 * to be throttled on pfmemalloc_wait as they should not be
  		 * able to safely make forward progress. Wake them
  		 */
  		if (waitqueue_active(&pgdat->pfmemalloc_wait) &&
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3574
  				allow_direct_reclaim(pgdat))
cfc511557   Vlastimil Babka   mm, vmscan: wake ...
3575
  			wake_up_all(&pgdat->pfmemalloc_wait);
5515061d2   Mel Gorman   mm: throttle dire...
3576

b8e83b942   Mel Gorman   mm: vmscan: flatt...
3577
  		/* Check if kswapd should be suspending */
93781325d   Omar Sandoval   lockdep: fix fs_r...
3578
3579
3580
3581
  		__fs_reclaim_release();
  		ret = try_to_freeze();
  		__fs_reclaim_acquire();
  		if (ret || kthread_should_stop())
b8e83b942   Mel Gorman   mm: vmscan: flatt...
3582
  			break;
8357376d3   Rafael J. Wysocki   [PATCH] swsusp: I...
3583

73ce02e96   KOSAKI Motohiro   mm: stop kswapd's...
3584
  		/*
b8e83b942   Mel Gorman   mm: vmscan: flatt...
3585
3586
  		 * Raise priority if scanning rate is too low or there was no
  		 * progress in reclaiming pages
73ce02e96   KOSAKI Motohiro   mm: stop kswapd's...
3587
  		 */
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3588
  		nr_reclaimed = sc.nr_reclaimed - nr_reclaimed;
1c30844d2   Mel Gorman   mm: reclaim small...
3589
3590
3591
3592
3593
3594
3595
3596
3597
  		nr_boost_reclaim -= min(nr_boost_reclaim, nr_reclaimed);
  
  		/*
  		 * If reclaim made no progress for a boost, stop reclaim as
  		 * IO cannot be queued and it could be an infinite loop in
  		 * extreme circumstances.
  		 */
  		if (nr_boost_reclaim && !nr_reclaimed)
  			break;
c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3598
  		if (raise_priority || !nr_reclaimed)
b8e83b942   Mel Gorman   mm: vmscan: flatt...
3599
  			sc.priority--;
1d82de618   Mel Gorman   mm, vmscan: make ...
3600
  	} while (sc.priority >= 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3601

c73322d09   Johannes Weiner   mm: fix 100% CPU ...
3602
3603
  	if (!sc.nr_reclaimed)
  		pgdat->kswapd_failures++;
b8e83b942   Mel Gorman   mm: vmscan: flatt...
3604
  out:
1c30844d2   Mel Gorman   mm: reclaim small...
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
  	/* If reclaim was boosted, account for the reclaim done in this pass */
  	if (boosted) {
  		unsigned long flags;
  
  		for (i = 0; i <= classzone_idx; i++) {
  			if (!zone_boosts[i])
  				continue;
  
  			/* Increments are under the zone lock */
  			zone = pgdat->node_zones + i;
  			spin_lock_irqsave(&zone->lock, flags);
  			zone->watermark_boost -= min(zone->watermark_boost, zone_boosts[i]);
  			spin_unlock_irqrestore(&zone->lock, flags);
  		}
  
  		/*
  		 * As there is now likely space, wakeup kcompact to defragment
  		 * pageblocks.
  		 */
  		wakeup_kcompactd(pgdat, pageblock_order, classzone_idx);
  	}
2a2e48854   Johannes Weiner   mm: vmscan: fix I...
3626
  	snapshot_refaults(NULL, pgdat);
93781325d   Omar Sandoval   lockdep: fix fs_r...
3627
  	__fs_reclaim_release();
eb414681d   Johannes Weiner   psi: pressure sta...
3628
  	psi_memstall_leave(&pflags);
1732d2b01   Andrew Morton   mm/vmscan.c: add ...
3629
  	set_task_reclaim_state(current, NULL);
e5ca8071f   Yafang Shao   mm/vmscan.c: add ...
3630

0abdee2bd   Mel Gorman   mm: kswapd: use t...
3631
  	/*
1d82de618   Mel Gorman   mm, vmscan: make ...
3632
3633
3634
3635
  	 * Return the order kswapd stopped reclaiming at as
  	 * prepare_kswapd_sleep() takes it into account. If another caller
  	 * entered the allocator slow path while kswapd was awake, order will
  	 * remain at the higher level.
0abdee2bd   Mel Gorman   mm: kswapd: use t...
3636
  	 */
1d82de618   Mel Gorman   mm, vmscan: make ...
3637
  	return sc.order;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3638
  }
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3639
  /*
dffcac2cb   Shakeel Butt   mm/vmscan.c: prev...
3640
3641
3642
3643
3644
   * The pgdat->kswapd_classzone_idx is used to pass the highest zone index to be
   * reclaimed by kswapd from the waker. If the value is MAX_NR_ZONES which is not
   * a valid index then either kswapd runs for first time or kswapd couldn't sleep
   * after previous reclaim attempt (node is still unbalanced). In that case
   * return the zone index of the previous kswapd reclaim cycle.
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3645
3646
   */
  static enum zone_type kswapd_classzone_idx(pg_data_t *pgdat,
dffcac2cb   Shakeel Butt   mm/vmscan.c: prev...
3647
  					   enum zone_type prev_classzone_idx)
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3648
  {
19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3649
3650
3651
  	enum zone_type curr_idx = READ_ONCE(pgdat->kswapd_classzone_idx);
  
  	return curr_idx == MAX_NR_ZONES ? prev_classzone_idx : curr_idx;
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3652
  }
38087d9b0   Mel Gorman   mm, vmscan: simpl...
3653
3654
  static void kswapd_try_to_sleep(pg_data_t *pgdat, int alloc_order, int reclaim_order,
  				unsigned int classzone_idx)
f0bc0a60b   KOSAKI Motohiro   vmscan: factor ou...
3655
3656
3657
3658
3659
3660
3661
3662
  {
  	long remaining = 0;
  	DEFINE_WAIT(wait);
  
  	if (freezing(current) || kthread_should_stop())
  		return;
  
  	prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
333b0a459   Shantanu Goel   mm, vmscan: fix z...
3663
3664
3665
3666
3667
3668
3669
  	/*
  	 * Try to sleep for a short interval. Note that kcompactd will only be
  	 * woken if it is possible to sleep for a short interval. This is
  	 * deliberate on the assumption that if reclaim cannot keep an
  	 * eligible zone balanced that it's also unlikely that compaction will
  	 * succeed.
  	 */
d9f21d426   Mel Gorman   mm, vmscan: avoid...
3670
  	if (prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
fd901c953   Vlastimil Babka   mm: wake kcompact...
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
  		/*
  		 * Compaction records what page blocks it recently failed to
  		 * isolate pages from and skips them in the future scanning.
  		 * When kswapd is going to sleep, it is reasonable to assume
  		 * that pages and compaction may succeed so reset the cache.
  		 */
  		reset_isolation_suitable(pgdat);
  
  		/*
  		 * We have freed the memory, now we should compact it to make
  		 * allocation of the requested order possible.
  		 */
38087d9b0   Mel Gorman   mm, vmscan: simpl...
3683
  		wakeup_kcompactd(pgdat, alloc_order, classzone_idx);
fd901c953   Vlastimil Babka   mm: wake kcompact...
3684

f0bc0a60b   KOSAKI Motohiro   vmscan: factor ou...
3685
  		remaining = schedule_timeout(HZ/10);
38087d9b0   Mel Gorman   mm, vmscan: simpl...
3686
3687
3688
3689
3690
3691
3692
  
  		/*
  		 * If woken prematurely then reset kswapd_classzone_idx and
  		 * order. The values will either be from a wakeup request or
  		 * the previous request that slept prematurely.
  		 */
  		if (remaining) {
19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3693
3694
3695
3696
3697
  			WRITE_ONCE(pgdat->kswapd_classzone_idx,
  				   kswapd_classzone_idx(pgdat, classzone_idx));
  
  			if (READ_ONCE(pgdat->kswapd_order) < reclaim_order)
  				WRITE_ONCE(pgdat->kswapd_order, reclaim_order);
38087d9b0   Mel Gorman   mm, vmscan: simpl...
3698
  		}
f0bc0a60b   KOSAKI Motohiro   vmscan: factor ou...
3699
3700
3701
3702
3703
3704
3705
3706
  		finish_wait(&pgdat->kswapd_wait, &wait);
  		prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE);
  	}
  
  	/*
  	 * After a short sleep, check if it was a premature sleep. If not, then
  	 * go fully to sleep until explicitly woken up.
  	 */
d9f21d426   Mel Gorman   mm, vmscan: avoid...
3707
3708
  	if (!remaining &&
  	    prepare_kswapd_sleep(pgdat, reclaim_order, classzone_idx)) {
f0bc0a60b   KOSAKI Motohiro   vmscan: factor ou...
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
  		trace_mm_vmscan_kswapd_sleep(pgdat->node_id);
  
  		/*
  		 * vmstat counters are not perfectly accurate and the estimated
  		 * value for counters such as NR_FREE_PAGES can deviate from the
  		 * true value by nr_online_cpus * threshold. To avoid the zone
  		 * watermarks being breached while under pressure, we reduce the
  		 * per-cpu vmstat threshold while kswapd is awake and restore
  		 * them before going back to sleep.
  		 */
  		set_pgdat_percpu_threshold(pgdat, calculate_normal_threshold);
1c7e7f6c0   Aaditya Kumar   mm: fix lost kswa...
3720
3721
3722
  
  		if (!kthread_should_stop())
  			schedule();
f0bc0a60b   KOSAKI Motohiro   vmscan: factor ou...
3723
3724
3725
3726
3727
3728
3729
3730
3731
  		set_pgdat_percpu_threshold(pgdat, calculate_pressure_threshold);
  	} else {
  		if (remaining)
  			count_vm_event(KSWAPD_LOW_WMARK_HIT_QUICKLY);
  		else
  			count_vm_event(KSWAPD_HIGH_WMARK_HIT_QUICKLY);
  	}
  	finish_wait(&pgdat->kswapd_wait, &wait);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3732
3733
  /*
   * The background pageout daemon, started as a kernel thread
4f98a2fee   Rik van Riel   vmscan: split LRU...
3734
   * from the init process.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
   *
   * This basically trickles out pages so that we have _some_
   * free memory available even if there is no other activity
   * that frees anything up. This is needed for things like routing
   * etc, where we otherwise might have all activity going on in
   * asynchronous contexts that cannot page things out.
   *
   * If there are applications that are active memory-allocators
   * (most normal use), this basically shouldn't matter.
   */
  static int kswapd(void *p)
  {
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3747
3748
  	unsigned int alloc_order, reclaim_order;
  	unsigned int classzone_idx = MAX_NR_ZONES - 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3749
3750
  	pg_data_t *pgdat = (pg_data_t*)p;
  	struct task_struct *tsk = current;
a70f73028   Rusty Russell   cpumask: replace ...
3751
  	const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3752

174596a0b   Rusty Russell   cpumask: convert mm/
3753
  	if (!cpumask_empty(cpumask))
c5f59f083   Mike Travis   nodemask: use new...
3754
  		set_cpus_allowed_ptr(tsk, cpumask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
  
  	/*
  	 * Tell the memory management that we're a "memory allocator",
  	 * and that if we need more memory we should get access to it
  	 * regardless (see "__alloc_pages()"). "kswapd" should
  	 * never get caught in the normal page freeing logic.
  	 *
  	 * (Kswapd normally doesn't need memory anyway, but sometimes
  	 * you need a small amount of memory in order to be able to
  	 * page out something else, and this flag essentially protects
  	 * us from recursively trying to free more memory as we're
  	 * trying to free the first piece of memory in the first place).
  	 */
930d91525   Christoph Lameter   [PATCH] Swap Migr...
3768
  	tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD;
831441862   Rafael J. Wysocki   Freezer: make ker...
3769
  	set_freezable();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3770

19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3771
3772
  	WRITE_ONCE(pgdat->kswapd_order, 0);
  	WRITE_ONCE(pgdat->kswapd_classzone_idx, MAX_NR_ZONES);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3773
  	for ( ; ; ) {
6f6313d48   Jeff Liu   mm/vmscan.c: try_...
3774
  		bool ret;
3e1d1d28d   Christoph Lameter   [PATCH] Cleanup p...
3775

19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3776
  		alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3777
  		classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
38087d9b0   Mel Gorman   mm, vmscan: simpl...
3778
3779
3780
  kswapd_try_sleep:
  		kswapd_try_to_sleep(pgdat, alloc_order, reclaim_order,
  					classzone_idx);
215ddd666   Mel Gorman   mm: vmscan: only ...
3781

38087d9b0   Mel Gorman   mm, vmscan: simpl...
3782
  		/* Read the new order and classzone_idx */
19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3783
  		alloc_order = reclaim_order = READ_ONCE(pgdat->kswapd_order);
dffcac2cb   Shakeel Butt   mm/vmscan.c: prev...
3784
  		classzone_idx = kswapd_classzone_idx(pgdat, classzone_idx);
19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3785
3786
  		WRITE_ONCE(pgdat->kswapd_order, 0);
  		WRITE_ONCE(pgdat->kswapd_classzone_idx, MAX_NR_ZONES);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3787

8fe23e057   David Rientjes   mm: clear node in...
3788
3789
3790
3791
3792
3793
3794
3795
  		ret = try_to_freeze();
  		if (kthread_should_stop())
  			break;
  
  		/*
  		 * We can speed up thawing tasks if we don't call balance_pgdat
  		 * after returning from the refrigerator
  		 */
38087d9b0   Mel Gorman   mm, vmscan: simpl...
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
  		if (ret)
  			continue;
  
  		/*
  		 * Reclaim begins at the requested order but if a high-order
  		 * reclaim fails then kswapd falls back to reclaiming for
  		 * order-0. If that happens, kswapd will consider sleeping
  		 * for the order it finished reclaiming at (reclaim_order)
  		 * but kcompactd is woken to compact for the original
  		 * request (alloc_order).
  		 */
e5146b12e   Mel Gorman   mm, vmscan: add c...
3807
3808
  		trace_mm_vmscan_kswapd_wake(pgdat->node_id, classzone_idx,
  						alloc_order);
38087d9b0   Mel Gorman   mm, vmscan: simpl...
3809
3810
3811
  		reclaim_order = balance_pgdat(pgdat, alloc_order, classzone_idx);
  		if (reclaim_order < alloc_order)
  			goto kswapd_try_sleep;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3812
  	}
b0a8cc58e   Takamori Yamaguchi   mm: bugfix: set c...
3813

71abdc15a   Johannes Weiner   mm: vmscan: clear...
3814
  	tsk->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD);
71abdc15a   Johannes Weiner   mm: vmscan: clear...
3815

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3816
3817
3818
3819
  	return 0;
  }
  
  /*
5ecd9d403   David Rientjes   mm, page_alloc: w...
3820
3821
3822
3823
3824
   * A zone is low on free memory or too fragmented for high-order memory.  If
   * kswapd should reclaim (direct reclaim is deferred), wake it up for the zone's
   * pgdat.  It will wake up kcompactd after reclaiming memory.  If kswapd reclaim
   * has failed or is not needed, still wake up kcompactd if only compaction is
   * needed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3825
   */
5ecd9d403   David Rientjes   mm, page_alloc: w...
3826
3827
  void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order,
  		   enum zone_type classzone_idx)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3828
3829
  {
  	pg_data_t *pgdat;
19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3830
  	enum zone_type curr_idx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3831

6aa303def   Mel Gorman   mm, vmscan: only ...
3832
  	if (!managed_zone(zone))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3833
  		return;
5ecd9d403   David Rientjes   mm, page_alloc: w...
3834
  	if (!cpuset_zone_allowed(zone, gfp_flags))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3835
  		return;
19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3836

88f5acf88   Mel Gorman   mm: page allocato...
3837
  	pgdat = zone->zone_pgdat;
19b9ff114   Qian Cai   mm/vmscan.c: fix ...
3838
3839
3840
3841
3842
3843
3844
  	curr_idx = READ_ONCE(pgdat->kswapd_classzone_idx);
  
  	if (curr_idx == MAX_NR_ZONES || curr_idx < classzone_idx)
  		WRITE_ONCE(pgdat->kswapd_classzone_idx, classzone_idx);
  
  	if (READ_ONCE(pgdat->kswapd_order) < order)
  		WRITE_ONCE(pgdat->kswapd_order, order);
dffcac2cb   Shakeel Butt   mm/vmscan.c: prev...
3845

8d0986e28   Con Kolivas   [PATCH] vm: kswap...
3846
  	if (!waitqueue_active(&pgdat->kswapd_wait))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3847
  		return;
e1a556374   Mel Gorman   mm, vmscan: only ...
3848

5ecd9d403   David Rientjes   mm, page_alloc: w...
3849
3850
  	/* Hopeless node, leave it to direct reclaim if possible */
  	if (pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ||
1c30844d2   Mel Gorman   mm: reclaim small...
3851
3852
  	    (pgdat_balanced(pgdat, order, classzone_idx) &&
  	     !pgdat_watermark_boosted(pgdat, classzone_idx))) {
5ecd9d403   David Rientjes   mm, page_alloc: w...
3853
3854
3855
3856
3857
3858
3859
3860
3861
  		/*
  		 * There may be plenty of free memory available, but it's too
  		 * fragmented for high-order allocations.  Wake up kcompactd
  		 * and rely on compaction_suitable() to determine if it's
  		 * needed.  If it fails, it will defer subsequent attempts to
  		 * ratelimit its work.
  		 */
  		if (!(gfp_flags & __GFP_DIRECT_RECLAIM))
  			wakeup_kcompactd(pgdat, order, classzone_idx);
e716f2eb2   Mel Gorman   mm, vmscan: preve...
3862
  		return;
5ecd9d403   David Rientjes   mm, page_alloc: w...
3863
  	}
88f5acf88   Mel Gorman   mm: page allocato...
3864

5ecd9d403   David Rientjes   mm, page_alloc: w...
3865
3866
  	trace_mm_vmscan_wakeup_kswapd(pgdat->node_id, classzone_idx, order,
  				      gfp_flags);
8d0986e28   Con Kolivas   [PATCH] vm: kswap...
3867
  	wake_up_interruptible(&pgdat->kswapd_wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3868
  }
c6f37f121   Rafael J. Wysocki   PM/Suspend: Do no...
3869
  #ifdef CONFIG_HIBERNATION
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3870
  /*
7b51755c3   KOSAKI Motohiro   vmscan: kill hibe...
3871
   * Try to free `nr_to_reclaim' of memory, system-wide, and return the number of
d6277db4a   Rafael J. Wysocki   [PATCH] swsusp: r...
3872
3873
3874
3875
3876
   * freed pages.
   *
   * Rather than trying to age LRUs the aim is to preserve the overall
   * LRU order by reclaiming preferentially
   * inactive > active > active referenced > active mapped
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3877
   */
7b51755c3   KOSAKI Motohiro   vmscan: kill hibe...
3878
  unsigned long shrink_all_memory(unsigned long nr_to_reclaim)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3879
  {
d6277db4a   Rafael J. Wysocki   [PATCH] swsusp: r...
3880
  	struct scan_control sc = {
ee814fe23   Johannes Weiner   mm: vmscan: clean...
3881
  		.nr_to_reclaim = nr_to_reclaim,
7b51755c3   KOSAKI Motohiro   vmscan: kill hibe...
3882
  		.gfp_mask = GFP_HIGHUSER_MOVABLE,
b2e18757f   Mel Gorman   mm, vmscan: begin...
3883
  		.reclaim_idx = MAX_NR_ZONES - 1,
ee814fe23   Johannes Weiner   mm: vmscan: clean...
3884
  		.priority = DEF_PRIORITY,
d6277db4a   Rafael J. Wysocki   [PATCH] swsusp: r...
3885
  		.may_writepage = 1,
ee814fe23   Johannes Weiner   mm: vmscan: clean...
3886
3887
  		.may_unmap = 1,
  		.may_swap = 1,
7b51755c3   KOSAKI Motohiro   vmscan: kill hibe...
3888
  		.hibernation_mode = 1,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3889
  	};
a09ed5e00   Ying Han   vmscan: change sh...
3890
  	struct zonelist *zonelist = node_zonelist(numa_node_id(), sc.gfp_mask);
7b51755c3   KOSAKI Motohiro   vmscan: kill hibe...
3891
  	unsigned long nr_reclaimed;
499118e96   Vlastimil Babka   mm: introduce mem...
3892
  	unsigned int noreclaim_flag;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3893

d92a8cfcb   Peter Zijlstra   locking/lockdep: ...
3894
  	fs_reclaim_acquire(sc.gfp_mask);
93781325d   Omar Sandoval   lockdep: fix fs_r...
3895
  	noreclaim_flag = memalloc_noreclaim_save();
1732d2b01   Andrew Morton   mm/vmscan.c: add ...
3896
  	set_task_reclaim_state(current, &sc.reclaim_state);
d6277db4a   Rafael J. Wysocki   [PATCH] swsusp: r...
3897

3115cd914   Vladimir Davydov   mm: vmscan: remov...
3898
  	nr_reclaimed = do_try_to_free_pages(zonelist, &sc);
d979677c4   MinChan Kim   mm: shrink_all_me...
3899

1732d2b01   Andrew Morton   mm/vmscan.c: add ...
3900
  	set_task_reclaim_state(current, NULL);
499118e96   Vlastimil Babka   mm: introduce mem...
3901
  	memalloc_noreclaim_restore(noreclaim_flag);
93781325d   Omar Sandoval   lockdep: fix fs_r...
3902
  	fs_reclaim_release(sc.gfp_mask);
d6277db4a   Rafael J. Wysocki   [PATCH] swsusp: r...
3903

7b51755c3   KOSAKI Motohiro   vmscan: kill hibe...
3904
  	return nr_reclaimed;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3905
  }
c6f37f121   Rafael J. Wysocki   PM/Suspend: Do no...
3906
  #endif /* CONFIG_HIBERNATION */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3907

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3908
3909
3910
3911
  /* It's optimal to keep kswapds on the same CPUs as their memory, but
     not required for correctness.  So if the last cpu in a node goes
     away, we get changed to run anywhere: as the first one comes back,
     restore their cpu bindings. */
517bbed90   Sebastian Andrzej Siewior   mm/vmscan: Conver...
3912
  static int kswapd_cpu_online(unsigned int cpu)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3913
  {
58c0a4a78   Yasunori Goto   Fix panic of cpu ...
3914
  	int nid;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3915

517bbed90   Sebastian Andrzej Siewior   mm/vmscan: Conver...
3916
3917
3918
  	for_each_node_state(nid, N_MEMORY) {
  		pg_data_t *pgdat = NODE_DATA(nid);
  		const struct cpumask *mask;
a70f73028   Rusty Russell   cpumask: replace ...
3919

517bbed90   Sebastian Andrzej Siewior   mm/vmscan: Conver...
3920
  		mask = cpumask_of_node(pgdat->node_id);
c5f59f083   Mike Travis   nodemask: use new...
3921

517bbed90   Sebastian Andrzej Siewior   mm/vmscan: Conver...
3922
3923
3924
  		if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids)
  			/* One of our CPUs online: restore mask */
  			set_cpus_allowed_ptr(pgdat->kswapd, mask);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3925
  	}
517bbed90   Sebastian Andrzej Siewior   mm/vmscan: Conver...
3926
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3927
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3928

3218ae14b   Yasunori Goto   [PATCH] pgdat all...
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
  /*
   * This kswapd start function will be called by init and node-hot-add.
   * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added.
   */
  int kswapd_run(int nid)
  {
  	pg_data_t *pgdat = NODE_DATA(nid);
  	int ret = 0;
  
  	if (pgdat->kswapd)
  		return 0;
  
  	pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid);
  	if (IS_ERR(pgdat->kswapd)) {
  		/* failure at boot is fatal */
c6202adf3   Thomas Gleixner   mm/vmscan: Adjust...
3944
  		BUG_ON(system_state < SYSTEM_RUNNING);
d5dc0ad92   Gavin Shan   mm/vmscan: fix er...
3945
3946
3947
  		pr_err("Failed to start kswapd on node %d
  ", nid);
  		ret = PTR_ERR(pgdat->kswapd);
d72515b85   Xishi Qiu   mm/vmscan: fix er...
3948
  		pgdat->kswapd = NULL;
3218ae14b   Yasunori Goto   [PATCH] pgdat all...
3949
3950
3951
  	}
  	return ret;
  }
8fe23e057   David Rientjes   mm: clear node in...
3952
  /*
d8adde17e   Jiang Liu   memory hotplug: f...
3953
   * Called by memory hotplug when all memory in a node is offlined.  Caller must
bfc8c9013   Vladimir Davydov   mem-hotplug: impl...
3954
   * hold mem_hotplug_begin/end().
8fe23e057   David Rientjes   mm: clear node in...
3955
3956
3957
3958
   */
  void kswapd_stop(int nid)
  {
  	struct task_struct *kswapd = NODE_DATA(nid)->kswapd;
d8adde17e   Jiang Liu   memory hotplug: f...
3959
  	if (kswapd) {
8fe23e057   David Rientjes   mm: clear node in...
3960
  		kthread_stop(kswapd);
d8adde17e   Jiang Liu   memory hotplug: f...
3961
3962
  		NODE_DATA(nid)->kswapd = NULL;
  	}
8fe23e057   David Rientjes   mm: clear node in...
3963
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3964
3965
  static int __init kswapd_init(void)
  {
517bbed90   Sebastian Andrzej Siewior   mm/vmscan: Conver...
3966
  	int nid, ret;
69e05944a   Andrew Morton   [PATCH] vmscan: u...
3967

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3968
  	swap_setup();
48fb2e240   Lai Jiangshan   vmscan: use N_MEM...
3969
  	for_each_node_state(nid, N_MEMORY)
3218ae14b   Yasunori Goto   [PATCH] pgdat all...
3970
   		kswapd_run(nid);
517bbed90   Sebastian Andrzej Siewior   mm/vmscan: Conver...
3971
3972
3973
3974
  	ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
  					"mm/vmscan:online", kswapd_cpu_online,
  					NULL);
  	WARN_ON(ret < 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3975
3976
3977
3978
  	return 0;
  }
  
  module_init(kswapd_init)
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
3979
3980
3981
  
  #ifdef CONFIG_NUMA
  /*
a5f5f91da   Mel Gorman   mm: convert zone_...
3982
   * Node reclaim mode
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
3983
   *
a5f5f91da   Mel Gorman   mm: convert zone_...
3984
   * If non-zero call node_reclaim when the number of free pages falls below
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
3985
   * the watermarks.
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
3986
   */
a5f5f91da   Mel Gorman   mm: convert zone_...
3987
  int node_reclaim_mode __read_mostly;
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
3988

1b2ffb789   Christoph Lameter   [PATCH] Zone recl...
3989
  #define RECLAIM_OFF 0
7d03431cf   Fernando Luis Vazquez Cao   swapfile/vmscan: ...
3990
  #define RECLAIM_ZONE (1<<0)	/* Run shrink_inactive_list on the zone */
1b2ffb789   Christoph Lameter   [PATCH] Zone recl...
3991
  #define RECLAIM_WRITE (1<<1)	/* Writeout pages during reclaim */
95bbc0c72   Zhihui Zhang   mm: rename RECLAI...
3992
  #define RECLAIM_UNMAP (1<<2)	/* Unmap pages during reclaim */
1b2ffb789   Christoph Lameter   [PATCH] Zone recl...
3993

9eeff2395   Christoph Lameter   [PATCH] Zone recl...
3994
  /*
a5f5f91da   Mel Gorman   mm: convert zone_...
3995
   * Priority for NODE_RECLAIM. This determines the fraction of pages
a92f71263   Christoph Lameter   [PATCH] zone_recl...
3996
3997
3998
   * of a node considered for each zone_reclaim. 4 scans 1/16th of
   * a zone.
   */
a5f5f91da   Mel Gorman   mm: convert zone_...
3999
  #define NODE_RECLAIM_PRIORITY 4
a92f71263   Christoph Lameter   [PATCH] zone_recl...
4000

9eeff2395   Christoph Lameter   [PATCH] Zone recl...
4001
  /*
a5f5f91da   Mel Gorman   mm: convert zone_...
4002
   * Percentage of pages in a zone that must be unmapped for node_reclaim to
9614634fe   Christoph Lameter   [PATCH] ZVC/zone_...
4003
4004
4005
4006
4007
   * occur.
   */
  int sysctl_min_unmapped_ratio = 1;
  
  /*
0ff38490c   Christoph Lameter   [PATCH] zone_recl...
4008
4009
4010
4011
   * If the number of slab pages in a zone grows beyond this percentage then
   * slab reclaim needs to occur.
   */
  int sysctl_min_slab_ratio = 5;
11fb99898   Mel Gorman   mm: move most fil...
4012
  static inline unsigned long node_unmapped_file_pages(struct pglist_data *pgdat)
90afa5de6   Mel Gorman   vmscan: properly ...
4013
  {
11fb99898   Mel Gorman   mm: move most fil...
4014
4015
4016
  	unsigned long file_mapped = node_page_state(pgdat, NR_FILE_MAPPED);
  	unsigned long file_lru = node_page_state(pgdat, NR_INACTIVE_FILE) +
  		node_page_state(pgdat, NR_ACTIVE_FILE);
90afa5de6   Mel Gorman   vmscan: properly ...
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
  
  	/*
  	 * It's possible for there to be more file mapped pages than
  	 * accounted for by the pages on the file LRU lists because
  	 * tmpfs pages accounted for as ANON can also be FILE_MAPPED
  	 */
  	return (file_lru > file_mapped) ? (file_lru - file_mapped) : 0;
  }
  
  /* Work out how many page cache pages we can reclaim in this reclaim_mode */
a5f5f91da   Mel Gorman   mm: convert zone_...
4027
  static unsigned long node_pagecache_reclaimable(struct pglist_data *pgdat)
90afa5de6   Mel Gorman   vmscan: properly ...
4028
  {
d031a1579   Alexandru Moise   mm/vmscan.c: fix ...
4029
4030
  	unsigned long nr_pagecache_reclaimable;
  	unsigned long delta = 0;
90afa5de6   Mel Gorman   vmscan: properly ...
4031
4032
  
  	/*
95bbc0c72   Zhihui Zhang   mm: rename RECLAI...
4033
  	 * If RECLAIM_UNMAP is set, then all file pages are considered
90afa5de6   Mel Gorman   vmscan: properly ...
4034
  	 * potentially reclaimable. Otherwise, we have to worry about
11fb99898   Mel Gorman   mm: move most fil...
4035
  	 * pages like swapcache and node_unmapped_file_pages() provides
90afa5de6   Mel Gorman   vmscan: properly ...
4036
4037
  	 * a better estimate
  	 */
a5f5f91da   Mel Gorman   mm: convert zone_...
4038
4039
  	if (node_reclaim_mode & RECLAIM_UNMAP)
  		nr_pagecache_reclaimable = node_page_state(pgdat, NR_FILE_PAGES);
90afa5de6   Mel Gorman   vmscan: properly ...
4040
  	else
a5f5f91da   Mel Gorman   mm: convert zone_...
4041
  		nr_pagecache_reclaimable = node_unmapped_file_pages(pgdat);
90afa5de6   Mel Gorman   vmscan: properly ...
4042
4043
  
  	/* If we can't clean pages, remove dirty pages from consideration */
a5f5f91da   Mel Gorman   mm: convert zone_...
4044
4045
  	if (!(node_reclaim_mode & RECLAIM_WRITE))
  		delta += node_page_state(pgdat, NR_FILE_DIRTY);
90afa5de6   Mel Gorman   vmscan: properly ...
4046
4047
4048
4049
4050
4051
4052
  
  	/* Watch for any possible underflows due to delta */
  	if (unlikely(delta > nr_pagecache_reclaimable))
  		delta = nr_pagecache_reclaimable;
  
  	return nr_pagecache_reclaimable - delta;
  }
0ff38490c   Christoph Lameter   [PATCH] zone_recl...
4053
  /*
a5f5f91da   Mel Gorman   mm: convert zone_...
4054
   * Try to free up some pages from this node through reclaim.
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
4055
   */
a5f5f91da   Mel Gorman   mm: convert zone_...
4056
  static int __node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
4057
  {
7fb2d46d3   Christoph Lameter   [PATCH] zone_recl...
4058
  	/* Minimum pages needed in order to stay on node */
69e05944a   Andrew Morton   [PATCH] vmscan: u...
4059
  	const unsigned long nr_pages = 1 << order;
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
4060
  	struct task_struct *p = current;
499118e96   Vlastimil Babka   mm: introduce mem...
4061
  	unsigned int noreclaim_flag;
179e96395   Andrew Morton   [PATCH] vmscan: s...
4062
  	struct scan_control sc = {
62b726c1b   Andrew Morton   mm/vmscan.c:__zon...
4063
  		.nr_to_reclaim = max(nr_pages, SWAP_CLUSTER_MAX),
f2f43e566   Nick Desaulniers   mm/vmscan.c: fix ...
4064
  		.gfp_mask = current_gfp_context(gfp_mask),
bd2f6199c   Johannes Weiner   vmscan: respect h...
4065
  		.order = order,
a5f5f91da   Mel Gorman   mm: convert zone_...
4066
4067
4068
  		.priority = NODE_RECLAIM_PRIORITY,
  		.may_writepage = !!(node_reclaim_mode & RECLAIM_WRITE),
  		.may_unmap = !!(node_reclaim_mode & RECLAIM_UNMAP),
ee814fe23   Johannes Weiner   mm: vmscan: clean...
4069
  		.may_swap = 1,
f2f43e566   Nick Desaulniers   mm/vmscan.c: fix ...
4070
  		.reclaim_idx = gfp_zone(gfp_mask),
179e96395   Andrew Morton   [PATCH] vmscan: s...
4071
  	};
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
4072

132bb8cfc   Yafang Shao   mm/vmscan: add tr...
4073
4074
  	trace_mm_vmscan_node_reclaim_begin(pgdat->node_id, order,
  					   sc.gfp_mask);
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
4075
  	cond_resched();
93781325d   Omar Sandoval   lockdep: fix fs_r...
4076
  	fs_reclaim_acquire(sc.gfp_mask);
d4f7796e9   Christoph Lameter   [PATCH] vmscan: f...
4077
  	/*
95bbc0c72   Zhihui Zhang   mm: rename RECLAI...
4078
  	 * We need to be able to allocate from the reserves for RECLAIM_UNMAP
d4f7796e9   Christoph Lameter   [PATCH] vmscan: f...
4079
  	 * and we also need to be able to write out pages for RECLAIM_WRITE
95bbc0c72   Zhihui Zhang   mm: rename RECLAI...
4080
  	 * and RECLAIM_UNMAP.
d4f7796e9   Christoph Lameter   [PATCH] vmscan: f...
4081
  	 */
499118e96   Vlastimil Babka   mm: introduce mem...
4082
4083
  	noreclaim_flag = memalloc_noreclaim_save();
  	p->flags |= PF_SWAPWRITE;
1732d2b01   Andrew Morton   mm/vmscan.c: add ...
4084
  	set_task_reclaim_state(p, &sc.reclaim_state);
c84db23c6   Christoph Lameter   [PATCH] zone_recl...
4085

a5f5f91da   Mel Gorman   mm: convert zone_...
4086
  	if (node_pagecache_reclaimable(pgdat) > pgdat->min_unmapped_pages) {
0ff38490c   Christoph Lameter   [PATCH] zone_recl...
4087
  		/*
894befec4   Andrey Ryabinin   mm/vmscan: update...
4088
  		 * Free memory by calling shrink node with increasing
0ff38490c   Christoph Lameter   [PATCH] zone_recl...
4089
4090
  		 * priorities until we have enough memory freed.
  		 */
0ff38490c   Christoph Lameter   [PATCH] zone_recl...
4091
  		do {
970a39a36   Mel Gorman   mm, vmscan: avoid...
4092
  			shrink_node(pgdat, &sc);
9e3b2f8cd   Konstantin Khlebnikov   mm/vmscan: store ...
4093
  		} while (sc.nr_reclaimed < nr_pages && --sc.priority >= 0);
0ff38490c   Christoph Lameter   [PATCH] zone_recl...
4094
  	}
c84db23c6   Christoph Lameter   [PATCH] zone_recl...
4095

1732d2b01   Andrew Morton   mm/vmscan.c: add ...
4096
  	set_task_reclaim_state(p, NULL);
499118e96   Vlastimil Babka   mm: introduce mem...
4097
4098
  	current->flags &= ~PF_SWAPWRITE;
  	memalloc_noreclaim_restore(noreclaim_flag);
93781325d   Omar Sandoval   lockdep: fix fs_r...
4099
  	fs_reclaim_release(sc.gfp_mask);
132bb8cfc   Yafang Shao   mm/vmscan: add tr...
4100
4101
  
  	trace_mm_vmscan_node_reclaim_end(sc.nr_reclaimed);
a79311c14   Rik van Riel   vmscan: bail out ...
4102
  	return sc.nr_reclaimed >= nr_pages;
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
4103
  }
179e96395   Andrew Morton   [PATCH] vmscan: s...
4104

a5f5f91da   Mel Gorman   mm: convert zone_...
4105
  int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
179e96395   Andrew Morton   [PATCH] vmscan: s...
4106
  {
d773ed6b8   David Rientjes   mm: test and set ...
4107
  	int ret;
179e96395   Andrew Morton   [PATCH] vmscan: s...
4108
4109
  
  	/*
a5f5f91da   Mel Gorman   mm: convert zone_...
4110
  	 * Node reclaim reclaims unmapped file backed pages and
0ff38490c   Christoph Lameter   [PATCH] zone_recl...
4111
  	 * slab pages if we are over the defined limits.
34aa1330f   Christoph Lameter   [PATCH] zoned vm ...
4112
  	 *
9614634fe   Christoph Lameter   [PATCH] ZVC/zone_...
4113
4114
  	 * A small portion of unmapped file backed pages is needed for
  	 * file I/O otherwise pages read by file I/O will be immediately
a5f5f91da   Mel Gorman   mm: convert zone_...
4115
4116
  	 * thrown out if the node is overallocated. So we do not reclaim
  	 * if less than a specified percentage of the node is used by
9614634fe   Christoph Lameter   [PATCH] ZVC/zone_...
4117
  	 * unmapped file backed pages.
179e96395   Andrew Morton   [PATCH] vmscan: s...
4118
  	 */
a5f5f91da   Mel Gorman   mm: convert zone_...
4119
  	if (node_pagecache_reclaimable(pgdat) <= pgdat->min_unmapped_pages &&
385386cff   Johannes Weiner   mm: vmstat: move ...
4120
  	    node_page_state(pgdat, NR_SLAB_RECLAIMABLE) <= pgdat->min_slab_pages)
a5f5f91da   Mel Gorman   mm: convert zone_...
4121
  		return NODE_RECLAIM_FULL;
179e96395   Andrew Morton   [PATCH] vmscan: s...
4122
4123
  
  	/*
d773ed6b8   David Rientjes   mm: test and set ...
4124
  	 * Do not scan if the allocation should not be delayed.
179e96395   Andrew Morton   [PATCH] vmscan: s...
4125
  	 */
d0164adc8   Mel Gorman   mm, page_alloc: d...
4126
  	if (!gfpflags_allow_blocking(gfp_mask) || (current->flags & PF_MEMALLOC))
a5f5f91da   Mel Gorman   mm: convert zone_...
4127
  		return NODE_RECLAIM_NOSCAN;
179e96395   Andrew Morton   [PATCH] vmscan: s...
4128
4129
  
  	/*
a5f5f91da   Mel Gorman   mm: convert zone_...
4130
  	 * Only run node reclaim on the local node or on nodes that do not
179e96395   Andrew Morton   [PATCH] vmscan: s...
4131
4132
4133
4134
  	 * have associated processors. This will favor the local processor
  	 * over remote processors and spread off node memory allocations
  	 * as wide as possible.
  	 */
a5f5f91da   Mel Gorman   mm: convert zone_...
4135
4136
  	if (node_state(pgdat->node_id, N_CPU) && pgdat->node_id != numa_node_id())
  		return NODE_RECLAIM_NOSCAN;
d773ed6b8   David Rientjes   mm: test and set ...
4137

a5f5f91da   Mel Gorman   mm: convert zone_...
4138
4139
  	if (test_and_set_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags))
  		return NODE_RECLAIM_NOSCAN;
fa5e084e4   Mel Gorman   vmscan: do not un...
4140

a5f5f91da   Mel Gorman   mm: convert zone_...
4141
4142
  	ret = __node_reclaim(pgdat, gfp_mask, order);
  	clear_bit(PGDAT_RECLAIM_LOCKED, &pgdat->flags);
d773ed6b8   David Rientjes   mm: test and set ...
4143

24cf72518   Mel Gorman   vmscan: count the...
4144
4145
  	if (!ret)
  		count_vm_event(PGSCAN_ZONE_RECLAIM_FAILED);
d773ed6b8   David Rientjes   mm: test and set ...
4146
  	return ret;
179e96395   Andrew Morton   [PATCH] vmscan: s...
4147
  }
9eeff2395   Christoph Lameter   [PATCH] Zone recl...
4148
  #endif
894bc3104   Lee Schermerhorn   Unevictable LRU I...
4149

894bc3104   Lee Schermerhorn   Unevictable LRU I...
4150
4151
4152
  /*
   * page_evictable - test whether a page is evictable
   * @page: the page to test
894bc3104   Lee Schermerhorn   Unevictable LRU I...
4153
4154
   *
   * Test whether page is evictable--i.e., should be placed on active/inactive
39b5f29ac   Hugh Dickins   mm: remove vma ar...
4155
   * lists vs unevictable list.
894bc3104   Lee Schermerhorn   Unevictable LRU I...
4156
4157
   *
   * Reasons page might not be evictable:
ba9ddf493   Lee Schermerhorn   Ramfs and Ram Dis...
4158
   * (1) page's mapping marked unevictable
b291f0003   Nick Piggin   mlock: mlocked pa...
4159
   * (2) page is part of an mlocked VMA
ba9ddf493   Lee Schermerhorn   Ramfs and Ram Dis...
4160
   *
894bc3104   Lee Schermerhorn   Unevictable LRU I...
4161
   */
39b5f29ac   Hugh Dickins   mm: remove vma ar...
4162
  int page_evictable(struct page *page)
894bc3104   Lee Schermerhorn   Unevictable LRU I...
4163
  {
e92bb4dd9   Huang Ying   mm: fix races bet...
4164
4165
4166
4167
4168
4169
4170
  	int ret;
  
  	/* Prevent address_space of inode and swap cache from being freed */
  	rcu_read_lock();
  	ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
  	rcu_read_unlock();
  	return ret;
894bc3104   Lee Schermerhorn   Unevictable LRU I...
4171
  }
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4172
4173
  
  /**
64e3d12f7   Kuo-Hsin Yang   mm, drm/i915: mar...
4174
4175
4176
   * check_move_unevictable_pages - check pages for evictability and move to
   * appropriate zone lru list
   * @pvec: pagevec with lru pages to check
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4177
   *
64e3d12f7   Kuo-Hsin Yang   mm, drm/i915: mar...
4178
4179
4180
   * Checks pages for evictability, if an evictable page is in the unevictable
   * lru list, moves it to the appropriate evictable lru list. This function
   * should be only used for lru pages.
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4181
   */
64e3d12f7   Kuo-Hsin Yang   mm, drm/i915: mar...
4182
  void check_move_unevictable_pages(struct pagevec *pvec)
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4183
  {
925b7673c   Johannes Weiner   mm: make per-memc...
4184
  	struct lruvec *lruvec;
785b99feb   Mel Gorman   mm, vmscan: relea...
4185
  	struct pglist_data *pgdat = NULL;
245132643   Hugh Dickins   SHM_UNLOCK: fix U...
4186
4187
4188
  	int pgscanned = 0;
  	int pgrescued = 0;
  	int i;
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4189

64e3d12f7   Kuo-Hsin Yang   mm, drm/i915: mar...
4190
4191
  	for (i = 0; i < pvec->nr; i++) {
  		struct page *page = pvec->pages[i];
785b99feb   Mel Gorman   mm, vmscan: relea...
4192
  		struct pglist_data *pagepgdat = page_pgdat(page);
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4193

245132643   Hugh Dickins   SHM_UNLOCK: fix U...
4194
  		pgscanned++;
785b99feb   Mel Gorman   mm, vmscan: relea...
4195
4196
4197
4198
4199
  		if (pagepgdat != pgdat) {
  			if (pgdat)
  				spin_unlock_irq(&pgdat->lru_lock);
  			pgdat = pagepgdat;
  			spin_lock_irq(&pgdat->lru_lock);
245132643   Hugh Dickins   SHM_UNLOCK: fix U...
4200
  		}
785b99feb   Mel Gorman   mm, vmscan: relea...
4201
  		lruvec = mem_cgroup_page_lruvec(page, pgdat);
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4202

245132643   Hugh Dickins   SHM_UNLOCK: fix U...
4203
4204
  		if (!PageLRU(page) || !PageUnevictable(page))
  			continue;
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4205

39b5f29ac   Hugh Dickins   mm: remove vma ar...
4206
  		if (page_evictable(page)) {
245132643   Hugh Dickins   SHM_UNLOCK: fix U...
4207
  			enum lru_list lru = page_lru_base_type(page);
309381fea   Sasha Levin   mm: dump page whe...
4208
  			VM_BUG_ON_PAGE(PageActive(page), page);
245132643   Hugh Dickins   SHM_UNLOCK: fix U...
4209
  			ClearPageUnevictable(page);
fa9add641   Hugh Dickins   mm/memcg: apply a...
4210
4211
  			del_page_from_lru_list(page, lruvec, LRU_UNEVICTABLE);
  			add_page_to_lru_list(page, lruvec, lru);
245132643   Hugh Dickins   SHM_UNLOCK: fix U...
4212
  			pgrescued++;
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4213
  		}
245132643   Hugh Dickins   SHM_UNLOCK: fix U...
4214
  	}
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4215

785b99feb   Mel Gorman   mm, vmscan: relea...
4216
  	if (pgdat) {
245132643   Hugh Dickins   SHM_UNLOCK: fix U...
4217
4218
  		__count_vm_events(UNEVICTABLE_PGRESCUED, pgrescued);
  		__count_vm_events(UNEVICTABLE_PGSCANNED, pgscanned);
785b99feb   Mel Gorman   mm, vmscan: relea...
4219
  		spin_unlock_irq(&pgdat->lru_lock);
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4220
  	}
89e004ea5   Lee Schermerhorn   SHM_LOCKED pages ...
4221
  }
64e3d12f7   Kuo-Hsin Yang   mm, drm/i915: mar...
4222
  EXPORT_SYMBOL_GPL(check_move_unevictable_pages);