Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/mm/vmscan.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | |
5 | * | |
6 | * Swap reorganised 29.12.95, Stephen Tweedie. | |
7 | * kswapd added: 7.1.96 sct | |
8 | * Removed kswapd_ctl limits, and swap out as many pages as needed | |
9 | * to bring the system back to freepages.high: 2.4.97, Rik van Riel. | |
10 | * Zone aware kswapd started 02/00, Kanoj Sarcar (kanoj@sgi.com). | |
11 | * Multiqueue VM started 5.8.00, Rik van Riel. | |
12 | */ | |
13 | ||
14 | #include <linux/mm.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/kernel_stat.h> | |
18 | #include <linux/swap.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/highmem.h> | |
e129b5c2 | 22 | #include <linux/vmstat.h> |
1da177e4 LT |
23 | #include <linux/file.h> |
24 | #include <linux/writeback.h> | |
25 | #include <linux/blkdev.h> | |
26 | #include <linux/buffer_head.h> /* for try_to_release_page(), | |
27 | buffer_heads_over_limit */ | |
28 | #include <linux/mm_inline.h> | |
29 | #include <linux/pagevec.h> | |
30 | #include <linux/backing-dev.h> | |
31 | #include <linux/rmap.h> | |
32 | #include <linux/topology.h> | |
33 | #include <linux/cpu.h> | |
34 | #include <linux/cpuset.h> | |
35 | #include <linux/notifier.h> | |
36 | #include <linux/rwsem.h> | |
248a0301 | 37 | #include <linux/delay.h> |
3218ae14 | 38 | #include <linux/kthread.h> |
7dfb7103 | 39 | #include <linux/freezer.h> |
66e1707b | 40 | #include <linux/memcontrol.h> |
873b4771 | 41 | #include <linux/delayacct.h> |
af936a16 | 42 | #include <linux/sysctl.h> |
1da177e4 LT |
43 | |
44 | #include <asm/tlbflush.h> | |
45 | #include <asm/div64.h> | |
46 | ||
47 | #include <linux/swapops.h> | |
48 | ||
0f8053a5 NP |
49 | #include "internal.h" |
50 | ||
1da177e4 | 51 | struct scan_control { |
1da177e4 LT |
52 | /* Incremented by the number of inactive pages that were scanned */ |
53 | unsigned long nr_scanned; | |
54 | ||
a79311c1 RR |
55 | /* Number of pages freed so far during a call to shrink_zones() */ |
56 | unsigned long nr_reclaimed; | |
57 | ||
1da177e4 | 58 | /* This context's GFP mask */ |
6daa0e28 | 59 | gfp_t gfp_mask; |
1da177e4 LT |
60 | |
61 | int may_writepage; | |
62 | ||
a6dc60f8 JW |
63 | /* Can mapped pages be reclaimed? */ |
64 | int may_unmap; | |
f1fd1067 | 65 | |
1da177e4 LT |
66 | /* This context's SWAP_CLUSTER_MAX. If freeing memory for |
67 | * suspend, we effectively ignore SWAP_CLUSTER_MAX. | |
68 | * In this context, it doesn't matter that we scan the | |
69 | * whole list at once. */ | |
70 | int swap_cluster_max; | |
d6277db4 RW |
71 | |
72 | int swappiness; | |
408d8544 NP |
73 | |
74 | int all_unreclaimable; | |
5ad333eb AW |
75 | |
76 | int order; | |
66e1707b BS |
77 | |
78 | /* Which cgroup do we reclaim from */ | |
79 | struct mem_cgroup *mem_cgroup; | |
80 | ||
81 | /* Pluggable isolate pages callback */ | |
82 | unsigned long (*isolate_pages)(unsigned long nr, struct list_head *dst, | |
83 | unsigned long *scanned, int order, int mode, | |
84 | struct zone *z, struct mem_cgroup *mem_cont, | |
4f98a2fe | 85 | int active, int file); |
1da177e4 LT |
86 | }; |
87 | ||
1da177e4 LT |
88 | #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) |
89 | ||
90 | #ifdef ARCH_HAS_PREFETCH | |
91 | #define prefetch_prev_lru_page(_page, _base, _field) \ | |
92 | do { \ | |
93 | if ((_page)->lru.prev != _base) { \ | |
94 | struct page *prev; \ | |
95 | \ | |
96 | prev = lru_to_page(&(_page->lru)); \ | |
97 | prefetch(&prev->_field); \ | |
98 | } \ | |
99 | } while (0) | |
100 | #else | |
101 | #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) | |
102 | #endif | |
103 | ||
104 | #ifdef ARCH_HAS_PREFETCHW | |
105 | #define prefetchw_prev_lru_page(_page, _base, _field) \ | |
106 | do { \ | |
107 | if ((_page)->lru.prev != _base) { \ | |
108 | struct page *prev; \ | |
109 | \ | |
110 | prev = lru_to_page(&(_page->lru)); \ | |
111 | prefetchw(&prev->_field); \ | |
112 | } \ | |
113 | } while (0) | |
114 | #else | |
115 | #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) | |
116 | #endif | |
117 | ||
118 | /* | |
119 | * From 0 .. 100. Higher means more swappy. | |
120 | */ | |
121 | int vm_swappiness = 60; | |
bd1e22b8 | 122 | long vm_total_pages; /* The total number of pages which the VM controls */ |
1da177e4 LT |
123 | |
124 | static LIST_HEAD(shrinker_list); | |
125 | static DECLARE_RWSEM(shrinker_rwsem); | |
126 | ||
00f0b825 | 127 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
e72e2bd6 | 128 | #define scanning_global_lru(sc) (!(sc)->mem_cgroup) |
91a45470 | 129 | #else |
e72e2bd6 | 130 | #define scanning_global_lru(sc) (1) |
91a45470 KH |
131 | #endif |
132 | ||
6e901571 KM |
133 | static struct zone_reclaim_stat *get_reclaim_stat(struct zone *zone, |
134 | struct scan_control *sc) | |
135 | { | |
e72e2bd6 | 136 | if (!scanning_global_lru(sc)) |
3e2f41f1 KM |
137 | return mem_cgroup_get_reclaim_stat(sc->mem_cgroup, zone); |
138 | ||
6e901571 KM |
139 | return &zone->reclaim_stat; |
140 | } | |
141 | ||
c9f299d9 KM |
142 | static unsigned long zone_nr_pages(struct zone *zone, struct scan_control *sc, |
143 | enum lru_list lru) | |
144 | { | |
e72e2bd6 | 145 | if (!scanning_global_lru(sc)) |
a3d8e054 KM |
146 | return mem_cgroup_zone_nr_pages(sc->mem_cgroup, zone, lru); |
147 | ||
c9f299d9 KM |
148 | return zone_page_state(zone, NR_LRU_BASE + lru); |
149 | } | |
150 | ||
151 | ||
1da177e4 LT |
152 | /* |
153 | * Add a shrinker callback to be called from the vm | |
154 | */ | |
8e1f936b | 155 | void register_shrinker(struct shrinker *shrinker) |
1da177e4 | 156 | { |
8e1f936b RR |
157 | shrinker->nr = 0; |
158 | down_write(&shrinker_rwsem); | |
159 | list_add_tail(&shrinker->list, &shrinker_list); | |
160 | up_write(&shrinker_rwsem); | |
1da177e4 | 161 | } |
8e1f936b | 162 | EXPORT_SYMBOL(register_shrinker); |
1da177e4 LT |
163 | |
164 | /* | |
165 | * Remove one | |
166 | */ | |
8e1f936b | 167 | void unregister_shrinker(struct shrinker *shrinker) |
1da177e4 LT |
168 | { |
169 | down_write(&shrinker_rwsem); | |
170 | list_del(&shrinker->list); | |
171 | up_write(&shrinker_rwsem); | |
1da177e4 | 172 | } |
8e1f936b | 173 | EXPORT_SYMBOL(unregister_shrinker); |
1da177e4 LT |
174 | |
175 | #define SHRINK_BATCH 128 | |
176 | /* | |
177 | * Call the shrink functions to age shrinkable caches | |
178 | * | |
179 | * Here we assume it costs one seek to replace a lru page and that it also | |
180 | * takes a seek to recreate a cache object. With this in mind we age equal | |
181 | * percentages of the lru and ageable caches. This should balance the seeks | |
182 | * generated by these structures. | |
183 | * | |
183ff22b | 184 | * If the vm encountered mapped pages on the LRU it increase the pressure on |
1da177e4 LT |
185 | * slab to avoid swapping. |
186 | * | |
187 | * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. | |
188 | * | |
189 | * `lru_pages' represents the number of on-LRU pages in all the zones which | |
190 | * are eligible for the caller's allocation attempt. It is used for balancing | |
191 | * slab reclaim versus page reclaim. | |
b15e0905 | 192 | * |
193 | * Returns the number of slab objects which we shrunk. | |
1da177e4 | 194 | */ |
69e05944 AM |
195 | unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask, |
196 | unsigned long lru_pages) | |
1da177e4 LT |
197 | { |
198 | struct shrinker *shrinker; | |
69e05944 | 199 | unsigned long ret = 0; |
1da177e4 LT |
200 | |
201 | if (scanned == 0) | |
202 | scanned = SWAP_CLUSTER_MAX; | |
203 | ||
204 | if (!down_read_trylock(&shrinker_rwsem)) | |
b15e0905 | 205 | return 1; /* Assume we'll be able to shrink next time */ |
1da177e4 LT |
206 | |
207 | list_for_each_entry(shrinker, &shrinker_list, list) { | |
208 | unsigned long long delta; | |
209 | unsigned long total_scan; | |
8e1f936b | 210 | unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask); |
1da177e4 LT |
211 | |
212 | delta = (4 * scanned) / shrinker->seeks; | |
ea164d73 | 213 | delta *= max_pass; |
1da177e4 LT |
214 | do_div(delta, lru_pages + 1); |
215 | shrinker->nr += delta; | |
ea164d73 | 216 | if (shrinker->nr < 0) { |
88c3bd70 DR |
217 | printk(KERN_ERR "shrink_slab: %pF negative objects to " |
218 | "delete nr=%ld\n", | |
219 | shrinker->shrink, shrinker->nr); | |
ea164d73 AA |
220 | shrinker->nr = max_pass; |
221 | } | |
222 | ||
223 | /* | |
224 | * Avoid risking looping forever due to too large nr value: | |
225 | * never try to free more than twice the estimate number of | |
226 | * freeable entries. | |
227 | */ | |
228 | if (shrinker->nr > max_pass * 2) | |
229 | shrinker->nr = max_pass * 2; | |
1da177e4 LT |
230 | |
231 | total_scan = shrinker->nr; | |
232 | shrinker->nr = 0; | |
233 | ||
234 | while (total_scan >= SHRINK_BATCH) { | |
235 | long this_scan = SHRINK_BATCH; | |
236 | int shrink_ret; | |
b15e0905 | 237 | int nr_before; |
1da177e4 | 238 | |
8e1f936b RR |
239 | nr_before = (*shrinker->shrink)(0, gfp_mask); |
240 | shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask); | |
1da177e4 LT |
241 | if (shrink_ret == -1) |
242 | break; | |
b15e0905 | 243 | if (shrink_ret < nr_before) |
244 | ret += nr_before - shrink_ret; | |
f8891e5e | 245 | count_vm_events(SLABS_SCANNED, this_scan); |
1da177e4 LT |
246 | total_scan -= this_scan; |
247 | ||
248 | cond_resched(); | |
249 | } | |
250 | ||
251 | shrinker->nr += total_scan; | |
252 | } | |
253 | up_read(&shrinker_rwsem); | |
b15e0905 | 254 | return ret; |
1da177e4 LT |
255 | } |
256 | ||
257 | /* Called without lock on whether page is mapped, so answer is unstable */ | |
258 | static inline int page_mapping_inuse(struct page *page) | |
259 | { | |
260 | struct address_space *mapping; | |
261 | ||
262 | /* Page is in somebody's page tables. */ | |
263 | if (page_mapped(page)) | |
264 | return 1; | |
265 | ||
266 | /* Be more reluctant to reclaim swapcache than pagecache */ | |
267 | if (PageSwapCache(page)) | |
268 | return 1; | |
269 | ||
270 | mapping = page_mapping(page); | |
271 | if (!mapping) | |
272 | return 0; | |
273 | ||
274 | /* File is mmap'd by somebody? */ | |
275 | return mapping_mapped(mapping); | |
276 | } | |
277 | ||
278 | static inline int is_page_cache_freeable(struct page *page) | |
279 | { | |
280 | return page_count(page) - !!PagePrivate(page) == 2; | |
281 | } | |
282 | ||
283 | static int may_write_to_queue(struct backing_dev_info *bdi) | |
284 | { | |
930d9152 | 285 | if (current->flags & PF_SWAPWRITE) |
1da177e4 LT |
286 | return 1; |
287 | if (!bdi_write_congested(bdi)) | |
288 | return 1; | |
289 | if (bdi == current->backing_dev_info) | |
290 | return 1; | |
291 | return 0; | |
292 | } | |
293 | ||
294 | /* | |
295 | * We detected a synchronous write error writing a page out. Probably | |
296 | * -ENOSPC. We need to propagate that into the address_space for a subsequent | |
297 | * fsync(), msync() or close(). | |
298 | * | |
299 | * The tricky part is that after writepage we cannot touch the mapping: nothing | |
300 | * prevents it from being freed up. But we have a ref on the page and once | |
301 | * that page is locked, the mapping is pinned. | |
302 | * | |
303 | * We're allowed to run sleeping lock_page() here because we know the caller has | |
304 | * __GFP_FS. | |
305 | */ | |
306 | static void handle_write_error(struct address_space *mapping, | |
307 | struct page *page, int error) | |
308 | { | |
309 | lock_page(page); | |
3e9f45bd GC |
310 | if (page_mapping(page) == mapping) |
311 | mapping_set_error(mapping, error); | |
1da177e4 LT |
312 | unlock_page(page); |
313 | } | |
314 | ||
c661b078 AW |
315 | /* Request for sync pageout. */ |
316 | enum pageout_io { | |
317 | PAGEOUT_IO_ASYNC, | |
318 | PAGEOUT_IO_SYNC, | |
319 | }; | |
320 | ||
04e62a29 CL |
321 | /* possible outcome of pageout() */ |
322 | typedef enum { | |
323 | /* failed to write page out, page is locked */ | |
324 | PAGE_KEEP, | |
325 | /* move page to the active list, page is locked */ | |
326 | PAGE_ACTIVATE, | |
327 | /* page has been sent to the disk successfully, page is unlocked */ | |
328 | PAGE_SUCCESS, | |
329 | /* page is clean and locked */ | |
330 | PAGE_CLEAN, | |
331 | } pageout_t; | |
332 | ||
1da177e4 | 333 | /* |
1742f19f AM |
334 | * pageout is called by shrink_page_list() for each dirty page. |
335 | * Calls ->writepage(). | |
1da177e4 | 336 | */ |
c661b078 AW |
337 | static pageout_t pageout(struct page *page, struct address_space *mapping, |
338 | enum pageout_io sync_writeback) | |
1da177e4 LT |
339 | { |
340 | /* | |
341 | * If the page is dirty, only perform writeback if that write | |
342 | * will be non-blocking. To prevent this allocation from being | |
343 | * stalled by pagecache activity. But note that there may be | |
344 | * stalls if we need to run get_block(). We could test | |
345 | * PagePrivate for that. | |
346 | * | |
347 | * If this process is currently in generic_file_write() against | |
348 | * this page's queue, we can perform writeback even if that | |
349 | * will block. | |
350 | * | |
351 | * If the page is swapcache, write it back even if that would | |
352 | * block, for some throttling. This happens by accident, because | |
353 | * swap_backing_dev_info is bust: it doesn't reflect the | |
354 | * congestion state of the swapdevs. Easy to fix, if needed. | |
355 | * See swapfile.c:page_queue_congested(). | |
356 | */ | |
357 | if (!is_page_cache_freeable(page)) | |
358 | return PAGE_KEEP; | |
359 | if (!mapping) { | |
360 | /* | |
361 | * Some data journaling orphaned pages can have | |
362 | * page->mapping == NULL while being dirty with clean buffers. | |
363 | */ | |
323aca6c | 364 | if (PagePrivate(page)) { |
1da177e4 LT |
365 | if (try_to_free_buffers(page)) { |
366 | ClearPageDirty(page); | |
d40cee24 | 367 | printk("%s: orphaned page\n", __func__); |
1da177e4 LT |
368 | return PAGE_CLEAN; |
369 | } | |
370 | } | |
371 | return PAGE_KEEP; | |
372 | } | |
373 | if (mapping->a_ops->writepage == NULL) | |
374 | return PAGE_ACTIVATE; | |
375 | if (!may_write_to_queue(mapping->backing_dev_info)) | |
376 | return PAGE_KEEP; | |
377 | ||
378 | if (clear_page_dirty_for_io(page)) { | |
379 | int res; | |
380 | struct writeback_control wbc = { | |
381 | .sync_mode = WB_SYNC_NONE, | |
382 | .nr_to_write = SWAP_CLUSTER_MAX, | |
111ebb6e OH |
383 | .range_start = 0, |
384 | .range_end = LLONG_MAX, | |
1da177e4 LT |
385 | .nonblocking = 1, |
386 | .for_reclaim = 1, | |
387 | }; | |
388 | ||
389 | SetPageReclaim(page); | |
390 | res = mapping->a_ops->writepage(page, &wbc); | |
391 | if (res < 0) | |
392 | handle_write_error(mapping, page, res); | |
994fc28c | 393 | if (res == AOP_WRITEPAGE_ACTIVATE) { |
1da177e4 LT |
394 | ClearPageReclaim(page); |
395 | return PAGE_ACTIVATE; | |
396 | } | |
c661b078 AW |
397 | |
398 | /* | |
399 | * Wait on writeback if requested to. This happens when | |
400 | * direct reclaiming a large contiguous area and the | |
401 | * first attempt to free a range of pages fails. | |
402 | */ | |
403 | if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC) | |
404 | wait_on_page_writeback(page); | |
405 | ||
1da177e4 LT |
406 | if (!PageWriteback(page)) { |
407 | /* synchronous write or broken a_ops? */ | |
408 | ClearPageReclaim(page); | |
409 | } | |
e129b5c2 | 410 | inc_zone_page_state(page, NR_VMSCAN_WRITE); |
1da177e4 LT |
411 | return PAGE_SUCCESS; |
412 | } | |
413 | ||
414 | return PAGE_CLEAN; | |
415 | } | |
416 | ||
a649fd92 | 417 | /* |
e286781d NP |
418 | * Same as remove_mapping, but if the page is removed from the mapping, it |
419 | * gets returned with a refcount of 0. | |
a649fd92 | 420 | */ |
e286781d | 421 | static int __remove_mapping(struct address_space *mapping, struct page *page) |
49d2e9cc | 422 | { |
28e4d965 NP |
423 | BUG_ON(!PageLocked(page)); |
424 | BUG_ON(mapping != page_mapping(page)); | |
49d2e9cc | 425 | |
19fd6231 | 426 | spin_lock_irq(&mapping->tree_lock); |
49d2e9cc | 427 | /* |
0fd0e6b0 NP |
428 | * The non racy check for a busy page. |
429 | * | |
430 | * Must be careful with the order of the tests. When someone has | |
431 | * a ref to the page, it may be possible that they dirty it then | |
432 | * drop the reference. So if PageDirty is tested before page_count | |
433 | * here, then the following race may occur: | |
434 | * | |
435 | * get_user_pages(&page); | |
436 | * [user mapping goes away] | |
437 | * write_to(page); | |
438 | * !PageDirty(page) [good] | |
439 | * SetPageDirty(page); | |
440 | * put_page(page); | |
441 | * !page_count(page) [good, discard it] | |
442 | * | |
443 | * [oops, our write_to data is lost] | |
444 | * | |
445 | * Reversing the order of the tests ensures such a situation cannot | |
446 | * escape unnoticed. The smp_rmb is needed to ensure the page->flags | |
447 | * load is not satisfied before that of page->_count. | |
448 | * | |
449 | * Note that if SetPageDirty is always performed via set_page_dirty, | |
450 | * and thus under tree_lock, then this ordering is not required. | |
49d2e9cc | 451 | */ |
e286781d | 452 | if (!page_freeze_refs(page, 2)) |
49d2e9cc | 453 | goto cannot_free; |
e286781d NP |
454 | /* note: atomic_cmpxchg in page_freeze_refs provides the smp_rmb */ |
455 | if (unlikely(PageDirty(page))) { | |
456 | page_unfreeze_refs(page, 2); | |
49d2e9cc | 457 | goto cannot_free; |
e286781d | 458 | } |
49d2e9cc CL |
459 | |
460 | if (PageSwapCache(page)) { | |
461 | swp_entry_t swap = { .val = page_private(page) }; | |
462 | __delete_from_swap_cache(page); | |
19fd6231 | 463 | spin_unlock_irq(&mapping->tree_lock); |
49d2e9cc | 464 | swap_free(swap); |
e286781d NP |
465 | } else { |
466 | __remove_from_page_cache(page); | |
19fd6231 | 467 | spin_unlock_irq(&mapping->tree_lock); |
49d2e9cc CL |
468 | } |
469 | ||
49d2e9cc CL |
470 | return 1; |
471 | ||
472 | cannot_free: | |
19fd6231 | 473 | spin_unlock_irq(&mapping->tree_lock); |
49d2e9cc CL |
474 | return 0; |
475 | } | |
476 | ||
e286781d NP |
477 | /* |
478 | * Attempt to detach a locked page from its ->mapping. If it is dirty or if | |
479 | * someone else has a ref on the page, abort and return 0. If it was | |
480 | * successfully detached, return 1. Assumes the caller has a single ref on | |
481 | * this page. | |
482 | */ | |
483 | int remove_mapping(struct address_space *mapping, struct page *page) | |
484 | { | |
485 | if (__remove_mapping(mapping, page)) { | |
486 | /* | |
487 | * Unfreezing the refcount with 1 rather than 2 effectively | |
488 | * drops the pagecache ref for us without requiring another | |
489 | * atomic operation. | |
490 | */ | |
491 | page_unfreeze_refs(page, 1); | |
492 | return 1; | |
493 | } | |
494 | return 0; | |
495 | } | |
496 | ||
894bc310 LS |
497 | /** |
498 | * putback_lru_page - put previously isolated page onto appropriate LRU list | |
499 | * @page: page to be put back to appropriate lru list | |
500 | * | |
501 | * Add previously isolated @page to appropriate LRU list. | |
502 | * Page may still be unevictable for other reasons. | |
503 | * | |
504 | * lru_lock must not be held, interrupts must be enabled. | |
505 | */ | |
506 | #ifdef CONFIG_UNEVICTABLE_LRU | |
507 | void putback_lru_page(struct page *page) | |
508 | { | |
509 | int lru; | |
510 | int active = !!TestClearPageActive(page); | |
bbfd28ee | 511 | int was_unevictable = PageUnevictable(page); |
894bc310 LS |
512 | |
513 | VM_BUG_ON(PageLRU(page)); | |
514 | ||
515 | redo: | |
516 | ClearPageUnevictable(page); | |
517 | ||
518 | if (page_evictable(page, NULL)) { | |
519 | /* | |
520 | * For evictable pages, we can use the cache. | |
521 | * In event of a race, worst case is we end up with an | |
522 | * unevictable page on [in]active list. | |
523 | * We know how to handle that. | |
524 | */ | |
525 | lru = active + page_is_file_cache(page); | |
526 | lru_cache_add_lru(page, lru); | |
527 | } else { | |
528 | /* | |
529 | * Put unevictable pages directly on zone's unevictable | |
530 | * list. | |
531 | */ | |
532 | lru = LRU_UNEVICTABLE; | |
533 | add_page_to_unevictable_list(page); | |
534 | } | |
894bc310 LS |
535 | |
536 | /* | |
537 | * page's status can change while we move it among lru. If an evictable | |
538 | * page is on unevictable list, it never be freed. To avoid that, | |
539 | * check after we added it to the list, again. | |
540 | */ | |
541 | if (lru == LRU_UNEVICTABLE && page_evictable(page, NULL)) { | |
542 | if (!isolate_lru_page(page)) { | |
543 | put_page(page); | |
544 | goto redo; | |
545 | } | |
546 | /* This means someone else dropped this page from LRU | |
547 | * So, it will be freed or putback to LRU again. There is | |
548 | * nothing to do here. | |
549 | */ | |
550 | } | |
551 | ||
bbfd28ee LS |
552 | if (was_unevictable && lru != LRU_UNEVICTABLE) |
553 | count_vm_event(UNEVICTABLE_PGRESCUED); | |
554 | else if (!was_unevictable && lru == LRU_UNEVICTABLE) | |
555 | count_vm_event(UNEVICTABLE_PGCULLED); | |
556 | ||
894bc310 LS |
557 | put_page(page); /* drop ref from isolate */ |
558 | } | |
559 | ||
560 | #else /* CONFIG_UNEVICTABLE_LRU */ | |
561 | ||
562 | void putback_lru_page(struct page *page) | |
563 | { | |
564 | int lru; | |
565 | VM_BUG_ON(PageLRU(page)); | |
566 | ||
567 | lru = !!TestClearPageActive(page) + page_is_file_cache(page); | |
568 | lru_cache_add_lru(page, lru); | |
894bc310 LS |
569 | put_page(page); |
570 | } | |
571 | #endif /* CONFIG_UNEVICTABLE_LRU */ | |
572 | ||
573 | ||
1da177e4 | 574 | /* |
1742f19f | 575 | * shrink_page_list() returns the number of reclaimed pages |
1da177e4 | 576 | */ |
1742f19f | 577 | static unsigned long shrink_page_list(struct list_head *page_list, |
c661b078 AW |
578 | struct scan_control *sc, |
579 | enum pageout_io sync_writeback) | |
1da177e4 LT |
580 | { |
581 | LIST_HEAD(ret_pages); | |
582 | struct pagevec freed_pvec; | |
583 | int pgactivate = 0; | |
05ff5137 | 584 | unsigned long nr_reclaimed = 0; |
1da177e4 LT |
585 | |
586 | cond_resched(); | |
587 | ||
588 | pagevec_init(&freed_pvec, 1); | |
589 | while (!list_empty(page_list)) { | |
590 | struct address_space *mapping; | |
591 | struct page *page; | |
592 | int may_enter_fs; | |
593 | int referenced; | |
594 | ||
595 | cond_resched(); | |
596 | ||
597 | page = lru_to_page(page_list); | |
598 | list_del(&page->lru); | |
599 | ||
529ae9aa | 600 | if (!trylock_page(page)) |
1da177e4 LT |
601 | goto keep; |
602 | ||
725d704e | 603 | VM_BUG_ON(PageActive(page)); |
1da177e4 LT |
604 | |
605 | sc->nr_scanned++; | |
80e43426 | 606 | |
b291f000 NP |
607 | if (unlikely(!page_evictable(page, NULL))) |
608 | goto cull_mlocked; | |
894bc310 | 609 | |
a6dc60f8 | 610 | if (!sc->may_unmap && page_mapped(page)) |
80e43426 CL |
611 | goto keep_locked; |
612 | ||
1da177e4 LT |
613 | /* Double the slab pressure for mapped and swapcache pages */ |
614 | if (page_mapped(page) || PageSwapCache(page)) | |
615 | sc->nr_scanned++; | |
616 | ||
c661b078 AW |
617 | may_enter_fs = (sc->gfp_mask & __GFP_FS) || |
618 | (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); | |
619 | ||
620 | if (PageWriteback(page)) { | |
621 | /* | |
622 | * Synchronous reclaim is performed in two passes, | |
623 | * first an asynchronous pass over the list to | |
624 | * start parallel writeback, and a second synchronous | |
625 | * pass to wait for the IO to complete. Wait here | |
626 | * for any page for which writeback has already | |
627 | * started. | |
628 | */ | |
629 | if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs) | |
630 | wait_on_page_writeback(page); | |
4dd4b920 | 631 | else |
c661b078 AW |
632 | goto keep_locked; |
633 | } | |
1da177e4 | 634 | |
bed7161a | 635 | referenced = page_referenced(page, 1, sc->mem_cgroup); |
1da177e4 | 636 | /* In active use or really unfreeable? Activate it. */ |
5ad333eb AW |
637 | if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && |
638 | referenced && page_mapping_inuse(page)) | |
1da177e4 LT |
639 | goto activate_locked; |
640 | ||
1da177e4 LT |
641 | /* |
642 | * Anonymous process memory has backing store? | |
643 | * Try to allocate it some swap space here. | |
644 | */ | |
b291f000 | 645 | if (PageAnon(page) && !PageSwapCache(page)) { |
63eb6b93 HD |
646 | if (!(sc->gfp_mask & __GFP_IO)) |
647 | goto keep_locked; | |
ac47b003 | 648 | if (!add_to_swap(page)) |
1da177e4 | 649 | goto activate_locked; |
63eb6b93 | 650 | may_enter_fs = 1; |
b291f000 | 651 | } |
1da177e4 LT |
652 | |
653 | mapping = page_mapping(page); | |
1da177e4 LT |
654 | |
655 | /* | |
656 | * The page is mapped into the page tables of one or more | |
657 | * processes. Try to unmap it here. | |
658 | */ | |
659 | if (page_mapped(page) && mapping) { | |
a48d07af | 660 | switch (try_to_unmap(page, 0)) { |
1da177e4 LT |
661 | case SWAP_FAIL: |
662 | goto activate_locked; | |
663 | case SWAP_AGAIN: | |
664 | goto keep_locked; | |
b291f000 NP |
665 | case SWAP_MLOCK: |
666 | goto cull_mlocked; | |
1da177e4 LT |
667 | case SWAP_SUCCESS: |
668 | ; /* try to free the page below */ | |
669 | } | |
670 | } | |
671 | ||
672 | if (PageDirty(page)) { | |
5ad333eb | 673 | if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced) |
1da177e4 | 674 | goto keep_locked; |
4dd4b920 | 675 | if (!may_enter_fs) |
1da177e4 | 676 | goto keep_locked; |
52a8363e | 677 | if (!sc->may_writepage) |
1da177e4 LT |
678 | goto keep_locked; |
679 | ||
680 | /* Page is dirty, try to write it out here */ | |
c661b078 | 681 | switch (pageout(page, mapping, sync_writeback)) { |
1da177e4 LT |
682 | case PAGE_KEEP: |
683 | goto keep_locked; | |
684 | case PAGE_ACTIVATE: | |
685 | goto activate_locked; | |
686 | case PAGE_SUCCESS: | |
4dd4b920 | 687 | if (PageWriteback(page) || PageDirty(page)) |
1da177e4 LT |
688 | goto keep; |
689 | /* | |
690 | * A synchronous write - probably a ramdisk. Go | |
691 | * ahead and try to reclaim the page. | |
692 | */ | |
529ae9aa | 693 | if (!trylock_page(page)) |
1da177e4 LT |
694 | goto keep; |
695 | if (PageDirty(page) || PageWriteback(page)) | |
696 | goto keep_locked; | |
697 | mapping = page_mapping(page); | |
698 | case PAGE_CLEAN: | |
699 | ; /* try to free the page below */ | |
700 | } | |
701 | } | |
702 | ||
703 | /* | |
704 | * If the page has buffers, try to free the buffer mappings | |
705 | * associated with this page. If we succeed we try to free | |
706 | * the page as well. | |
707 | * | |
708 | * We do this even if the page is PageDirty(). | |
709 | * try_to_release_page() does not perform I/O, but it is | |
710 | * possible for a page to have PageDirty set, but it is actually | |
711 | * clean (all its buffers are clean). This happens if the | |
712 | * buffers were written out directly, with submit_bh(). ext3 | |
894bc310 | 713 | * will do this, as well as the blockdev mapping. |
1da177e4 LT |
714 | * try_to_release_page() will discover that cleanness and will |
715 | * drop the buffers and mark the page clean - it can be freed. | |
716 | * | |
717 | * Rarely, pages can have buffers and no ->mapping. These are | |
718 | * the pages which were not successfully invalidated in | |
719 | * truncate_complete_page(). We try to drop those buffers here | |
720 | * and if that worked, and the page is no longer mapped into | |
721 | * process address space (page_count == 1) it can be freed. | |
722 | * Otherwise, leave the page on the LRU so it is swappable. | |
723 | */ | |
724 | if (PagePrivate(page)) { | |
725 | if (!try_to_release_page(page, sc->gfp_mask)) | |
726 | goto activate_locked; | |
e286781d NP |
727 | if (!mapping && page_count(page) == 1) { |
728 | unlock_page(page); | |
729 | if (put_page_testzero(page)) | |
730 | goto free_it; | |
731 | else { | |
732 | /* | |
733 | * rare race with speculative reference. | |
734 | * the speculative reference will free | |
735 | * this page shortly, so we may | |
736 | * increment nr_reclaimed here (and | |
737 | * leave it off the LRU). | |
738 | */ | |
739 | nr_reclaimed++; | |
740 | continue; | |
741 | } | |
742 | } | |
1da177e4 LT |
743 | } |
744 | ||
e286781d | 745 | if (!mapping || !__remove_mapping(mapping, page)) |
49d2e9cc | 746 | goto keep_locked; |
1da177e4 | 747 | |
a978d6f5 NP |
748 | /* |
749 | * At this point, we have no other references and there is | |
750 | * no way to pick any more up (removed from LRU, removed | |
751 | * from pagecache). Can use non-atomic bitops now (and | |
752 | * we obviously don't have to worry about waking up a process | |
753 | * waiting on the page lock, because there are no references. | |
754 | */ | |
755 | __clear_page_locked(page); | |
e286781d | 756 | free_it: |
05ff5137 | 757 | nr_reclaimed++; |
e286781d NP |
758 | if (!pagevec_add(&freed_pvec, page)) { |
759 | __pagevec_free(&freed_pvec); | |
760 | pagevec_reinit(&freed_pvec); | |
761 | } | |
1da177e4 LT |
762 | continue; |
763 | ||
b291f000 | 764 | cull_mlocked: |
63d6c5ad HD |
765 | if (PageSwapCache(page)) |
766 | try_to_free_swap(page); | |
b291f000 NP |
767 | unlock_page(page); |
768 | putback_lru_page(page); | |
769 | continue; | |
770 | ||
1da177e4 | 771 | activate_locked: |
68a22394 RR |
772 | /* Not a candidate for swapping, so reclaim swap space. */ |
773 | if (PageSwapCache(page) && vm_swap_full()) | |
a2c43eed | 774 | try_to_free_swap(page); |
894bc310 | 775 | VM_BUG_ON(PageActive(page)); |
1da177e4 LT |
776 | SetPageActive(page); |
777 | pgactivate++; | |
778 | keep_locked: | |
779 | unlock_page(page); | |
780 | keep: | |
781 | list_add(&page->lru, &ret_pages); | |
b291f000 | 782 | VM_BUG_ON(PageLRU(page) || PageUnevictable(page)); |
1da177e4 LT |
783 | } |
784 | list_splice(&ret_pages, page_list); | |
785 | if (pagevec_count(&freed_pvec)) | |
e286781d | 786 | __pagevec_free(&freed_pvec); |
f8891e5e | 787 | count_vm_events(PGACTIVATE, pgactivate); |
05ff5137 | 788 | return nr_reclaimed; |
1da177e4 LT |
789 | } |
790 | ||
5ad333eb AW |
791 | /* LRU Isolation modes. */ |
792 | #define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */ | |
793 | #define ISOLATE_ACTIVE 1 /* Isolate active pages. */ | |
794 | #define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */ | |
795 | ||
796 | /* | |
797 | * Attempt to remove the specified page from its LRU. Only take this page | |
798 | * if it is of the appropriate PageActive status. Pages which are being | |
799 | * freed elsewhere are also ignored. | |
800 | * | |
801 | * page: page to consider | |
802 | * mode: one of the LRU isolation modes defined above | |
803 | * | |
804 | * returns 0 on success, -ve errno on failure. | |
805 | */ | |
4f98a2fe | 806 | int __isolate_lru_page(struct page *page, int mode, int file) |
5ad333eb AW |
807 | { |
808 | int ret = -EINVAL; | |
809 | ||
810 | /* Only take pages on the LRU. */ | |
811 | if (!PageLRU(page)) | |
812 | return ret; | |
813 | ||
814 | /* | |
815 | * When checking the active state, we need to be sure we are | |
816 | * dealing with comparible boolean values. Take the logical not | |
817 | * of each. | |
818 | */ | |
819 | if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode)) | |
820 | return ret; | |
821 | ||
4f98a2fe RR |
822 | if (mode != ISOLATE_BOTH && (!page_is_file_cache(page) != !file)) |
823 | return ret; | |
824 | ||
894bc310 LS |
825 | /* |
826 | * When this function is being called for lumpy reclaim, we | |
827 | * initially look into all LRU pages, active, inactive and | |
828 | * unevictable; only give shrink_page_list evictable pages. | |
829 | */ | |
830 | if (PageUnevictable(page)) | |
831 | return ret; | |
832 | ||
5ad333eb | 833 | ret = -EBUSY; |
08e552c6 | 834 | |
5ad333eb AW |
835 | if (likely(get_page_unless_zero(page))) { |
836 | /* | |
837 | * Be careful not to clear PageLRU until after we're | |
838 | * sure the page is not being freed elsewhere -- the | |
839 | * page release code relies on it. | |
840 | */ | |
841 | ClearPageLRU(page); | |
842 | ret = 0; | |
08e552c6 | 843 | mem_cgroup_del_lru(page); |
5ad333eb AW |
844 | } |
845 | ||
846 | return ret; | |
847 | } | |
848 | ||
1da177e4 LT |
849 | /* |
850 | * zone->lru_lock is heavily contended. Some of the functions that | |
851 | * shrink the lists perform better by taking out a batch of pages | |
852 | * and working on them outside the LRU lock. | |
853 | * | |
854 | * For pagecache intensive workloads, this function is the hottest | |
855 | * spot in the kernel (apart from copy_*_user functions). | |
856 | * | |
857 | * Appropriate locks must be held before calling this function. | |
858 | * | |
859 | * @nr_to_scan: The number of pages to look through on the list. | |
860 | * @src: The LRU list to pull pages off. | |
861 | * @dst: The temp list to put pages on to. | |
862 | * @scanned: The number of pages that were scanned. | |
5ad333eb AW |
863 | * @order: The caller's attempted allocation order |
864 | * @mode: One of the LRU isolation modes | |
4f98a2fe | 865 | * @file: True [1] if isolating file [!anon] pages |
1da177e4 LT |
866 | * |
867 | * returns how many pages were moved onto *@dst. | |
868 | */ | |
69e05944 AM |
869 | static unsigned long isolate_lru_pages(unsigned long nr_to_scan, |
870 | struct list_head *src, struct list_head *dst, | |
4f98a2fe | 871 | unsigned long *scanned, int order, int mode, int file) |
1da177e4 | 872 | { |
69e05944 | 873 | unsigned long nr_taken = 0; |
c9b02d97 | 874 | unsigned long scan; |
1da177e4 | 875 | |
c9b02d97 | 876 | for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { |
5ad333eb AW |
877 | struct page *page; |
878 | unsigned long pfn; | |
879 | unsigned long end_pfn; | |
880 | unsigned long page_pfn; | |
881 | int zone_id; | |
882 | ||
1da177e4 LT |
883 | page = lru_to_page(src); |
884 | prefetchw_prev_lru_page(page, src, flags); | |
885 | ||
725d704e | 886 | VM_BUG_ON(!PageLRU(page)); |
8d438f96 | 887 | |
4f98a2fe | 888 | switch (__isolate_lru_page(page, mode, file)) { |
5ad333eb AW |
889 | case 0: |
890 | list_move(&page->lru, dst); | |
7c8ee9a8 | 891 | nr_taken++; |
5ad333eb AW |
892 | break; |
893 | ||
894 | case -EBUSY: | |
895 | /* else it is being freed elsewhere */ | |
896 | list_move(&page->lru, src); | |
897 | continue; | |
46453a6e | 898 | |
5ad333eb AW |
899 | default: |
900 | BUG(); | |
901 | } | |
902 | ||
903 | if (!order) | |
904 | continue; | |
905 | ||
906 | /* | |
907 | * Attempt to take all pages in the order aligned region | |
908 | * surrounding the tag page. Only take those pages of | |
909 | * the same active state as that tag page. We may safely | |
910 | * round the target page pfn down to the requested order | |
911 | * as the mem_map is guarenteed valid out to MAX_ORDER, | |
912 | * where that page is in a different zone we will detect | |
913 | * it from its zone id and abort this block scan. | |
914 | */ | |
915 | zone_id = page_zone_id(page); | |
916 | page_pfn = page_to_pfn(page); | |
917 | pfn = page_pfn & ~((1 << order) - 1); | |
918 | end_pfn = pfn + (1 << order); | |
919 | for (; pfn < end_pfn; pfn++) { | |
920 | struct page *cursor_page; | |
921 | ||
922 | /* The target page is in the block, ignore it. */ | |
923 | if (unlikely(pfn == page_pfn)) | |
924 | continue; | |
925 | ||
926 | /* Avoid holes within the zone. */ | |
927 | if (unlikely(!pfn_valid_within(pfn))) | |
928 | break; | |
929 | ||
930 | cursor_page = pfn_to_page(pfn); | |
4f98a2fe | 931 | |
5ad333eb AW |
932 | /* Check that we have not crossed a zone boundary. */ |
933 | if (unlikely(page_zone_id(cursor_page) != zone_id)) | |
934 | continue; | |
4f98a2fe | 935 | switch (__isolate_lru_page(cursor_page, mode, file)) { |
5ad333eb AW |
936 | case 0: |
937 | list_move(&cursor_page->lru, dst); | |
938 | nr_taken++; | |
939 | scan++; | |
940 | break; | |
941 | ||
942 | case -EBUSY: | |
943 | /* else it is being freed elsewhere */ | |
944 | list_move(&cursor_page->lru, src); | |
945 | default: | |
894bc310 | 946 | break; /* ! on LRU or wrong list */ |
5ad333eb AW |
947 | } |
948 | } | |
1da177e4 LT |
949 | } |
950 | ||
951 | *scanned = scan; | |
952 | return nr_taken; | |
953 | } | |
954 | ||
66e1707b BS |
955 | static unsigned long isolate_pages_global(unsigned long nr, |
956 | struct list_head *dst, | |
957 | unsigned long *scanned, int order, | |
958 | int mode, struct zone *z, | |
959 | struct mem_cgroup *mem_cont, | |
4f98a2fe | 960 | int active, int file) |
66e1707b | 961 | { |
4f98a2fe | 962 | int lru = LRU_BASE; |
66e1707b | 963 | if (active) |
4f98a2fe RR |
964 | lru += LRU_ACTIVE; |
965 | if (file) | |
966 | lru += LRU_FILE; | |
967 | return isolate_lru_pages(nr, &z->lru[lru].list, dst, scanned, order, | |
968 | mode, !!file); | |
66e1707b BS |
969 | } |
970 | ||
5ad333eb AW |
971 | /* |
972 | * clear_active_flags() is a helper for shrink_active_list(), clearing | |
973 | * any active bits from the pages in the list. | |
974 | */ | |
4f98a2fe RR |
975 | static unsigned long clear_active_flags(struct list_head *page_list, |
976 | unsigned int *count) | |
5ad333eb AW |
977 | { |
978 | int nr_active = 0; | |
4f98a2fe | 979 | int lru; |
5ad333eb AW |
980 | struct page *page; |
981 | ||
4f98a2fe RR |
982 | list_for_each_entry(page, page_list, lru) { |
983 | lru = page_is_file_cache(page); | |
5ad333eb | 984 | if (PageActive(page)) { |
4f98a2fe | 985 | lru += LRU_ACTIVE; |
5ad333eb AW |
986 | ClearPageActive(page); |
987 | nr_active++; | |
988 | } | |
4f98a2fe RR |
989 | count[lru]++; |
990 | } | |
5ad333eb AW |
991 | |
992 | return nr_active; | |
993 | } | |
994 | ||
62695a84 NP |
995 | /** |
996 | * isolate_lru_page - tries to isolate a page from its LRU list | |
997 | * @page: page to isolate from its LRU list | |
998 | * | |
999 | * Isolates a @page from an LRU list, clears PageLRU and adjusts the | |
1000 | * vmstat statistic corresponding to whatever LRU list the page was on. | |
1001 | * | |
1002 | * Returns 0 if the page was removed from an LRU list. | |
1003 | * Returns -EBUSY if the page was not on an LRU list. | |
1004 | * | |
1005 | * The returned page will have PageLRU() cleared. If it was found on | |
894bc310 LS |
1006 | * the active list, it will have PageActive set. If it was found on |
1007 | * the unevictable list, it will have the PageUnevictable bit set. That flag | |
1008 | * may need to be cleared by the caller before letting the page go. | |
62695a84 NP |
1009 | * |
1010 | * The vmstat statistic corresponding to the list on which the page was | |
1011 | * found will be decremented. | |
1012 | * | |
1013 | * Restrictions: | |
1014 | * (1) Must be called with an elevated refcount on the page. This is a | |
1015 | * fundamentnal difference from isolate_lru_pages (which is called | |
1016 | * without a stable reference). | |
1017 | * (2) the lru_lock must not be held. | |
1018 | * (3) interrupts must be enabled. | |
1019 | */ | |
1020 | int isolate_lru_page(struct page *page) | |
1021 | { | |
1022 | int ret = -EBUSY; | |
1023 | ||
1024 | if (PageLRU(page)) { | |
1025 | struct zone *zone = page_zone(page); | |
1026 | ||
1027 | spin_lock_irq(&zone->lru_lock); | |
1028 | if (PageLRU(page) && get_page_unless_zero(page)) { | |
894bc310 | 1029 | int lru = page_lru(page); |
62695a84 NP |
1030 | ret = 0; |
1031 | ClearPageLRU(page); | |
4f98a2fe | 1032 | |
4f98a2fe | 1033 | del_page_from_lru_list(zone, page, lru); |
62695a84 NP |
1034 | } |
1035 | spin_unlock_irq(&zone->lru_lock); | |
1036 | } | |
1037 | return ret; | |
1038 | } | |
1039 | ||
1da177e4 | 1040 | /* |
1742f19f AM |
1041 | * shrink_inactive_list() is a helper for shrink_zone(). It returns the number |
1042 | * of reclaimed pages | |
1da177e4 | 1043 | */ |
1742f19f | 1044 | static unsigned long shrink_inactive_list(unsigned long max_scan, |
33c120ed RR |
1045 | struct zone *zone, struct scan_control *sc, |
1046 | int priority, int file) | |
1da177e4 LT |
1047 | { |
1048 | LIST_HEAD(page_list); | |
1049 | struct pagevec pvec; | |
69e05944 | 1050 | unsigned long nr_scanned = 0; |
05ff5137 | 1051 | unsigned long nr_reclaimed = 0; |
6e901571 | 1052 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
1da177e4 LT |
1053 | |
1054 | pagevec_init(&pvec, 1); | |
1055 | ||
1056 | lru_add_drain(); | |
1057 | spin_lock_irq(&zone->lru_lock); | |
69e05944 | 1058 | do { |
1da177e4 | 1059 | struct page *page; |
69e05944 AM |
1060 | unsigned long nr_taken; |
1061 | unsigned long nr_scan; | |
1062 | unsigned long nr_freed; | |
5ad333eb | 1063 | unsigned long nr_active; |
4f98a2fe | 1064 | unsigned int count[NR_LRU_LISTS] = { 0, }; |
33c120ed RR |
1065 | int mode = ISOLATE_INACTIVE; |
1066 | ||
1067 | /* | |
1068 | * If we need a large contiguous chunk of memory, or have | |
1069 | * trouble getting a small set of contiguous pages, we | |
1070 | * will reclaim both active and inactive pages. | |
1071 | * | |
1072 | * We use the same threshold as pageout congestion_wait below. | |
1073 | */ | |
1074 | if (sc->order > PAGE_ALLOC_COSTLY_ORDER) | |
1075 | mode = ISOLATE_BOTH; | |
1076 | else if (sc->order && priority < DEF_PRIORITY - 2) | |
1077 | mode = ISOLATE_BOTH; | |
1da177e4 | 1078 | |
66e1707b | 1079 | nr_taken = sc->isolate_pages(sc->swap_cluster_max, |
4f98a2fe RR |
1080 | &page_list, &nr_scan, sc->order, mode, |
1081 | zone, sc->mem_cgroup, 0, file); | |
1082 | nr_active = clear_active_flags(&page_list, count); | |
e9187bdc | 1083 | __count_vm_events(PGDEACTIVATE, nr_active); |
5ad333eb | 1084 | |
4f98a2fe RR |
1085 | __mod_zone_page_state(zone, NR_ACTIVE_FILE, |
1086 | -count[LRU_ACTIVE_FILE]); | |
1087 | __mod_zone_page_state(zone, NR_INACTIVE_FILE, | |
1088 | -count[LRU_INACTIVE_FILE]); | |
1089 | __mod_zone_page_state(zone, NR_ACTIVE_ANON, | |
1090 | -count[LRU_ACTIVE_ANON]); | |
1091 | __mod_zone_page_state(zone, NR_INACTIVE_ANON, | |
1092 | -count[LRU_INACTIVE_ANON]); | |
1093 | ||
e72e2bd6 | 1094 | if (scanning_global_lru(sc)) |
1cfb419b | 1095 | zone->pages_scanned += nr_scan; |
3e2f41f1 KM |
1096 | |
1097 | reclaim_stat->recent_scanned[0] += count[LRU_INACTIVE_ANON]; | |
1098 | reclaim_stat->recent_scanned[0] += count[LRU_ACTIVE_ANON]; | |
1099 | reclaim_stat->recent_scanned[1] += count[LRU_INACTIVE_FILE]; | |
1100 | reclaim_stat->recent_scanned[1] += count[LRU_ACTIVE_FILE]; | |
1101 | ||
1da177e4 LT |
1102 | spin_unlock_irq(&zone->lru_lock); |
1103 | ||
69e05944 | 1104 | nr_scanned += nr_scan; |
c661b078 AW |
1105 | nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC); |
1106 | ||
1107 | /* | |
1108 | * If we are direct reclaiming for contiguous pages and we do | |
1109 | * not reclaim everything in the list, try again and wait | |
1110 | * for IO to complete. This will stall high-order allocations | |
1111 | * but that should be acceptable to the caller | |
1112 | */ | |
1113 | if (nr_freed < nr_taken && !current_is_kswapd() && | |
1114 | sc->order > PAGE_ALLOC_COSTLY_ORDER) { | |
1115 | congestion_wait(WRITE, HZ/10); | |
1116 | ||
1117 | /* | |
1118 | * The attempt at page out may have made some | |
1119 | * of the pages active, mark them inactive again. | |
1120 | */ | |
4f98a2fe | 1121 | nr_active = clear_active_flags(&page_list, count); |
c661b078 AW |
1122 | count_vm_events(PGDEACTIVATE, nr_active); |
1123 | ||
1124 | nr_freed += shrink_page_list(&page_list, sc, | |
1125 | PAGEOUT_IO_SYNC); | |
1126 | } | |
1127 | ||
05ff5137 | 1128 | nr_reclaimed += nr_freed; |
a74609fa NP |
1129 | local_irq_disable(); |
1130 | if (current_is_kswapd()) { | |
f8891e5e CL |
1131 | __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan); |
1132 | __count_vm_events(KSWAPD_STEAL, nr_freed); | |
e72e2bd6 | 1133 | } else if (scanning_global_lru(sc)) |
f8891e5e | 1134 | __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan); |
1cfb419b | 1135 | |
918d3f90 | 1136 | __count_zone_vm_events(PGSTEAL, zone, nr_freed); |
a74609fa | 1137 | |
fb8d14e1 WF |
1138 | if (nr_taken == 0) |
1139 | goto done; | |
1140 | ||
a74609fa | 1141 | spin_lock(&zone->lru_lock); |
1da177e4 LT |
1142 | /* |
1143 | * Put back any unfreeable pages. | |
1144 | */ | |
1145 | while (!list_empty(&page_list)) { | |
894bc310 | 1146 | int lru; |
1da177e4 | 1147 | page = lru_to_page(&page_list); |
725d704e | 1148 | VM_BUG_ON(PageLRU(page)); |
1da177e4 | 1149 | list_del(&page->lru); |
894bc310 LS |
1150 | if (unlikely(!page_evictable(page, NULL))) { |
1151 | spin_unlock_irq(&zone->lru_lock); | |
1152 | putback_lru_page(page); | |
1153 | spin_lock_irq(&zone->lru_lock); | |
1154 | continue; | |
1155 | } | |
1156 | SetPageLRU(page); | |
1157 | lru = page_lru(page); | |
1158 | add_page_to_lru_list(zone, page, lru); | |
3e2f41f1 | 1159 | if (PageActive(page)) { |
4f98a2fe | 1160 | int file = !!page_is_file_cache(page); |
6e901571 | 1161 | reclaim_stat->recent_rotated[file]++; |
4f98a2fe | 1162 | } |
1da177e4 LT |
1163 | if (!pagevec_add(&pvec, page)) { |
1164 | spin_unlock_irq(&zone->lru_lock); | |
1165 | __pagevec_release(&pvec); | |
1166 | spin_lock_irq(&zone->lru_lock); | |
1167 | } | |
1168 | } | |
69e05944 | 1169 | } while (nr_scanned < max_scan); |
fb8d14e1 | 1170 | spin_unlock(&zone->lru_lock); |
1da177e4 | 1171 | done: |
fb8d14e1 | 1172 | local_irq_enable(); |
1da177e4 | 1173 | pagevec_release(&pvec); |
05ff5137 | 1174 | return nr_reclaimed; |
1da177e4 LT |
1175 | } |
1176 | ||
3bb1a852 MB |
1177 | /* |
1178 | * We are about to scan this zone at a certain priority level. If that priority | |
1179 | * level is smaller (ie: more urgent) than the previous priority, then note | |
1180 | * that priority level within the zone. This is done so that when the next | |
1181 | * process comes in to scan this zone, it will immediately start out at this | |
1182 | * priority level rather than having to build up its own scanning priority. | |
1183 | * Here, this priority affects only the reclaim-mapped threshold. | |
1184 | */ | |
1185 | static inline void note_zone_scanning_priority(struct zone *zone, int priority) | |
1186 | { | |
1187 | if (priority < zone->prev_priority) | |
1188 | zone->prev_priority = priority; | |
1189 | } | |
1190 | ||
1da177e4 LT |
1191 | /* |
1192 | * This moves pages from the active list to the inactive list. | |
1193 | * | |
1194 | * We move them the other way if the page is referenced by one or more | |
1195 | * processes, from rmap. | |
1196 | * | |
1197 | * If the pages are mostly unmapped, the processing is fast and it is | |
1198 | * appropriate to hold zone->lru_lock across the whole operation. But if | |
1199 | * the pages are mapped, the processing is slow (page_referenced()) so we | |
1200 | * should drop zone->lru_lock around each page. It's impossible to balance | |
1201 | * this, so instead we remove the pages from the LRU while processing them. | |
1202 | * It is safe to rely on PG_active against the non-LRU pages in here because | |
1203 | * nobody will play with that bit on a non-LRU page. | |
1204 | * | |
1205 | * The downside is that we have to touch page->_count against each page. | |
1206 | * But we had to alter page->flags anyway. | |
1207 | */ | |
1cfb419b KH |
1208 | |
1209 | ||
1742f19f | 1210 | static void shrink_active_list(unsigned long nr_pages, struct zone *zone, |
4f98a2fe | 1211 | struct scan_control *sc, int priority, int file) |
1da177e4 | 1212 | { |
69e05944 | 1213 | unsigned long pgmoved; |
1da177e4 | 1214 | int pgdeactivate = 0; |
69e05944 | 1215 | unsigned long pgscanned; |
1da177e4 | 1216 | LIST_HEAD(l_hold); /* The pages which were snipped off */ |
b69408e8 | 1217 | LIST_HEAD(l_inactive); |
1da177e4 LT |
1218 | struct page *page; |
1219 | struct pagevec pvec; | |
4f98a2fe | 1220 | enum lru_list lru; |
6e901571 | 1221 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
1da177e4 LT |
1222 | |
1223 | lru_add_drain(); | |
1224 | spin_lock_irq(&zone->lru_lock); | |
66e1707b BS |
1225 | pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order, |
1226 | ISOLATE_ACTIVE, zone, | |
4f98a2fe | 1227 | sc->mem_cgroup, 1, file); |
1cfb419b KH |
1228 | /* |
1229 | * zone->pages_scanned is used for detect zone's oom | |
1230 | * mem_cgroup remembers nr_scan by itself. | |
1231 | */ | |
e72e2bd6 | 1232 | if (scanning_global_lru(sc)) { |
1cfb419b | 1233 | zone->pages_scanned += pgscanned; |
4f98a2fe | 1234 | } |
3e2f41f1 | 1235 | reclaim_stat->recent_scanned[!!file] += pgmoved; |
1cfb419b | 1236 | |
4f98a2fe RR |
1237 | if (file) |
1238 | __mod_zone_page_state(zone, NR_ACTIVE_FILE, -pgmoved); | |
1239 | else | |
1240 | __mod_zone_page_state(zone, NR_ACTIVE_ANON, -pgmoved); | |
1da177e4 LT |
1241 | spin_unlock_irq(&zone->lru_lock); |
1242 | ||
556adecb | 1243 | pgmoved = 0; |
1da177e4 LT |
1244 | while (!list_empty(&l_hold)) { |
1245 | cond_resched(); | |
1246 | page = lru_to_page(&l_hold); | |
1247 | list_del(&page->lru); | |
7e9cd484 | 1248 | |
894bc310 LS |
1249 | if (unlikely(!page_evictable(page, NULL))) { |
1250 | putback_lru_page(page); | |
1251 | continue; | |
1252 | } | |
1253 | ||
7e9cd484 RR |
1254 | /* page_referenced clears PageReferenced */ |
1255 | if (page_mapping_inuse(page) && | |
1256 | page_referenced(page, 0, sc->mem_cgroup)) | |
1257 | pgmoved++; | |
1258 | ||
1da177e4 LT |
1259 | list_add(&page->lru, &l_inactive); |
1260 | } | |
1261 | ||
b555749a AM |
1262 | /* |
1263 | * Move the pages to the [file or anon] inactive list. | |
1264 | */ | |
1265 | pagevec_init(&pvec, 1); | |
b555749a AM |
1266 | lru = LRU_BASE + file * LRU_FILE; |
1267 | ||
2a1dc509 | 1268 | spin_lock_irq(&zone->lru_lock); |
556adecb | 1269 | /* |
7e9cd484 RR |
1270 | * Count referenced pages from currently used mappings as |
1271 | * rotated, even though they are moved to the inactive list. | |
1272 | * This helps balance scan pressure between file and anonymous | |
1273 | * pages in get_scan_ratio. | |
1274 | */ | |
3e2f41f1 | 1275 | reclaim_stat->recent_rotated[!!file] += pgmoved; |
556adecb | 1276 | |
1d885526 | 1277 | pgmoved = 0; |
1da177e4 LT |
1278 | while (!list_empty(&l_inactive)) { |
1279 | page = lru_to_page(&l_inactive); | |
1280 | prefetchw_prev_lru_page(page, &l_inactive, flags); | |
725d704e | 1281 | VM_BUG_ON(PageLRU(page)); |
8d438f96 | 1282 | SetPageLRU(page); |
725d704e | 1283 | VM_BUG_ON(!PageActive(page)); |
4c84cacf NP |
1284 | ClearPageActive(page); |
1285 | ||
4f98a2fe | 1286 | list_move(&page->lru, &zone->lru[lru].list); |
08e552c6 | 1287 | mem_cgroup_add_lru_list(page, lru); |
1da177e4 LT |
1288 | pgmoved++; |
1289 | if (!pagevec_add(&pvec, page)) { | |
4f98a2fe | 1290 | __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); |
1da177e4 LT |
1291 | spin_unlock_irq(&zone->lru_lock); |
1292 | pgdeactivate += pgmoved; | |
1293 | pgmoved = 0; | |
1294 | if (buffer_heads_over_limit) | |
1295 | pagevec_strip(&pvec); | |
1296 | __pagevec_release(&pvec); | |
1297 | spin_lock_irq(&zone->lru_lock); | |
1298 | } | |
1299 | } | |
4f98a2fe | 1300 | __mod_zone_page_state(zone, NR_LRU_BASE + lru, pgmoved); |
1da177e4 | 1301 | pgdeactivate += pgmoved; |
f8891e5e CL |
1302 | __count_zone_vm_events(PGREFILL, zone, pgscanned); |
1303 | __count_vm_events(PGDEACTIVATE, pgdeactivate); | |
1304 | spin_unlock_irq(&zone->lru_lock); | |
2443462b JW |
1305 | if (buffer_heads_over_limit) |
1306 | pagevec_strip(&pvec); | |
a74609fa | 1307 | pagevec_release(&pvec); |
1da177e4 LT |
1308 | } |
1309 | ||
14797e23 | 1310 | static int inactive_anon_is_low_global(struct zone *zone) |
f89eb90e KM |
1311 | { |
1312 | unsigned long active, inactive; | |
1313 | ||
1314 | active = zone_page_state(zone, NR_ACTIVE_ANON); | |
1315 | inactive = zone_page_state(zone, NR_INACTIVE_ANON); | |
1316 | ||
1317 | if (inactive * zone->inactive_ratio < active) | |
1318 | return 1; | |
1319 | ||
1320 | return 0; | |
1321 | } | |
1322 | ||
14797e23 KM |
1323 | /** |
1324 | * inactive_anon_is_low - check if anonymous pages need to be deactivated | |
1325 | * @zone: zone to check | |
1326 | * @sc: scan control of this context | |
1327 | * | |
1328 | * Returns true if the zone does not have enough inactive anon pages, | |
1329 | * meaning some active anon pages need to be deactivated. | |
1330 | */ | |
1331 | static int inactive_anon_is_low(struct zone *zone, struct scan_control *sc) | |
1332 | { | |
1333 | int low; | |
1334 | ||
e72e2bd6 | 1335 | if (scanning_global_lru(sc)) |
14797e23 KM |
1336 | low = inactive_anon_is_low_global(zone); |
1337 | else | |
c772be93 | 1338 | low = mem_cgroup_inactive_anon_is_low(sc->mem_cgroup); |
14797e23 KM |
1339 | return low; |
1340 | } | |
1341 | ||
4f98a2fe | 1342 | static unsigned long shrink_list(enum lru_list lru, unsigned long nr_to_scan, |
b69408e8 CL |
1343 | struct zone *zone, struct scan_control *sc, int priority) |
1344 | { | |
4f98a2fe RR |
1345 | int file = is_file_lru(lru); |
1346 | ||
556adecb RR |
1347 | if (lru == LRU_ACTIVE_FILE) { |
1348 | shrink_active_list(nr_to_scan, zone, sc, priority, file); | |
1349 | return 0; | |
1350 | } | |
1351 | ||
14797e23 | 1352 | if (lru == LRU_ACTIVE_ANON && inactive_anon_is_low(zone, sc)) { |
4f98a2fe | 1353 | shrink_active_list(nr_to_scan, zone, sc, priority, file); |
b69408e8 CL |
1354 | return 0; |
1355 | } | |
33c120ed | 1356 | return shrink_inactive_list(nr_to_scan, zone, sc, priority, file); |
4f98a2fe RR |
1357 | } |
1358 | ||
1359 | /* | |
1360 | * Determine how aggressively the anon and file LRU lists should be | |
1361 | * scanned. The relative value of each set of LRU lists is determined | |
1362 | * by looking at the fraction of the pages scanned we did rotate back | |
1363 | * onto the active list instead of evict. | |
1364 | * | |
1365 | * percent[0] specifies how much pressure to put on ram/swap backed | |
1366 | * memory, while percent[1] determines pressure on the file LRUs. | |
1367 | */ | |
1368 | static void get_scan_ratio(struct zone *zone, struct scan_control *sc, | |
1369 | unsigned long *percent) | |
1370 | { | |
1371 | unsigned long anon, file, free; | |
1372 | unsigned long anon_prio, file_prio; | |
1373 | unsigned long ap, fp; | |
6e901571 | 1374 | struct zone_reclaim_stat *reclaim_stat = get_reclaim_stat(zone, sc); |
4f98a2fe | 1375 | |
4f98a2fe RR |
1376 | /* If we have no swap space, do not bother scanning anon pages. */ |
1377 | if (nr_swap_pages <= 0) { | |
1378 | percent[0] = 0; | |
1379 | percent[1] = 100; | |
1380 | return; | |
1381 | } | |
1382 | ||
c9f299d9 KM |
1383 | anon = zone_nr_pages(zone, sc, LRU_ACTIVE_ANON) + |
1384 | zone_nr_pages(zone, sc, LRU_INACTIVE_ANON); | |
1385 | file = zone_nr_pages(zone, sc, LRU_ACTIVE_FILE) + | |
1386 | zone_nr_pages(zone, sc, LRU_INACTIVE_FILE); | |
b962716b | 1387 | |
e72e2bd6 | 1388 | if (scanning_global_lru(sc)) { |
eeee9a8c KM |
1389 | free = zone_page_state(zone, NR_FREE_PAGES); |
1390 | /* If we have very few page cache pages, | |
1391 | force-scan anon pages. */ | |
1392 | if (unlikely(file + free <= zone->pages_high)) { | |
1393 | percent[0] = 100; | |
1394 | percent[1] = 0; | |
1395 | return; | |
1396 | } | |
4f98a2fe RR |
1397 | } |
1398 | ||
1399 | /* | |
1400 | * OK, so we have swap space and a fair amount of page cache | |
1401 | * pages. We use the recently rotated / recently scanned | |
1402 | * ratios to determine how valuable each cache is. | |
1403 | * | |
1404 | * Because workloads change over time (and to avoid overflow) | |
1405 | * we keep these statistics as a floating average, which ends | |
1406 | * up weighing recent references more than old ones. | |
1407 | * | |
1408 | * anon in [0], file in [1] | |
1409 | */ | |
6e901571 | 1410 | if (unlikely(reclaim_stat->recent_scanned[0] > anon / 4)) { |
4f98a2fe | 1411 | spin_lock_irq(&zone->lru_lock); |
6e901571 KM |
1412 | reclaim_stat->recent_scanned[0] /= 2; |
1413 | reclaim_stat->recent_rotated[0] /= 2; | |
4f98a2fe RR |
1414 | spin_unlock_irq(&zone->lru_lock); |
1415 | } | |
1416 | ||
6e901571 | 1417 | if (unlikely(reclaim_stat->recent_scanned[1] > file / 4)) { |
4f98a2fe | 1418 | spin_lock_irq(&zone->lru_lock); |
6e901571 KM |
1419 | reclaim_stat->recent_scanned[1] /= 2; |
1420 | reclaim_stat->recent_rotated[1] /= 2; | |
4f98a2fe RR |
1421 | spin_unlock_irq(&zone->lru_lock); |
1422 | } | |
1423 | ||
1424 | /* | |
1425 | * With swappiness at 100, anonymous and file have the same priority. | |
1426 | * This scanning priority is essentially the inverse of IO cost. | |
1427 | */ | |
1428 | anon_prio = sc->swappiness; | |
1429 | file_prio = 200 - sc->swappiness; | |
1430 | ||
1431 | /* | |
00d8089c RR |
1432 | * The amount of pressure on anon vs file pages is inversely |
1433 | * proportional to the fraction of recently scanned pages on | |
1434 | * each list that were recently referenced and in active use. | |
4f98a2fe | 1435 | */ |
6e901571 KM |
1436 | ap = (anon_prio + 1) * (reclaim_stat->recent_scanned[0] + 1); |
1437 | ap /= reclaim_stat->recent_rotated[0] + 1; | |
4f98a2fe | 1438 | |
6e901571 KM |
1439 | fp = (file_prio + 1) * (reclaim_stat->recent_scanned[1] + 1); |
1440 | fp /= reclaim_stat->recent_rotated[1] + 1; | |
4f98a2fe RR |
1441 | |
1442 | /* Normalize to percentages */ | |
1443 | percent[0] = 100 * ap / (ap + fp + 1); | |
1444 | percent[1] = 100 - percent[0]; | |
b69408e8 CL |
1445 | } |
1446 | ||
4f98a2fe | 1447 | |
1da177e4 LT |
1448 | /* |
1449 | * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. | |
1450 | */ | |
a79311c1 | 1451 | static void shrink_zone(int priority, struct zone *zone, |
05ff5137 | 1452 | struct scan_control *sc) |
1da177e4 | 1453 | { |
b69408e8 | 1454 | unsigned long nr[NR_LRU_LISTS]; |
8695949a | 1455 | unsigned long nr_to_scan; |
4f98a2fe | 1456 | unsigned long percent[2]; /* anon @ 0; file @ 1 */ |
b69408e8 | 1457 | enum lru_list l; |
01dbe5c9 KM |
1458 | unsigned long nr_reclaimed = sc->nr_reclaimed; |
1459 | unsigned long swap_cluster_max = sc->swap_cluster_max; | |
1da177e4 | 1460 | |
4f98a2fe RR |
1461 | get_scan_ratio(zone, sc, percent); |
1462 | ||
894bc310 | 1463 | for_each_evictable_lru(l) { |
9439c1c9 KM |
1464 | int file = is_file_lru(l); |
1465 | int scan; | |
e0f79b8f | 1466 | |
f272b7bc | 1467 | scan = zone_nr_pages(zone, sc, l); |
9439c1c9 KM |
1468 | if (priority) { |
1469 | scan >>= priority; | |
1470 | scan = (scan * percent[file]) / 100; | |
1471 | } | |
e72e2bd6 | 1472 | if (scanning_global_lru(sc)) { |
e0f79b8f | 1473 | zone->lru[l].nr_scan += scan; |
b69408e8 | 1474 | nr[l] = zone->lru[l].nr_scan; |
01dbe5c9 | 1475 | if (nr[l] >= swap_cluster_max) |
b69408e8 CL |
1476 | zone->lru[l].nr_scan = 0; |
1477 | else | |
1478 | nr[l] = 0; | |
9439c1c9 KM |
1479 | } else |
1480 | nr[l] = scan; | |
1cfb419b | 1481 | } |
1da177e4 | 1482 | |
556adecb RR |
1483 | while (nr[LRU_INACTIVE_ANON] || nr[LRU_ACTIVE_FILE] || |
1484 | nr[LRU_INACTIVE_FILE]) { | |
894bc310 | 1485 | for_each_evictable_lru(l) { |
b69408e8 | 1486 | if (nr[l]) { |
01dbe5c9 | 1487 | nr_to_scan = min(nr[l], swap_cluster_max); |
b69408e8 | 1488 | nr[l] -= nr_to_scan; |
1da177e4 | 1489 | |
01dbe5c9 KM |
1490 | nr_reclaimed += shrink_list(l, nr_to_scan, |
1491 | zone, sc, priority); | |
b69408e8 | 1492 | } |
1da177e4 | 1493 | } |
a79311c1 RR |
1494 | /* |
1495 | * On large memory systems, scan >> priority can become | |
1496 | * really large. This is fine for the starting priority; | |
1497 | * we want to put equal scanning pressure on each zone. | |
1498 | * However, if the VM has a harder time of freeing pages, | |
1499 | * with multiple processes reclaiming pages, the total | |
1500 | * freeing target can get unreasonably large. | |
1501 | */ | |
01dbe5c9 | 1502 | if (nr_reclaimed > swap_cluster_max && |
a79311c1 RR |
1503 | priority < DEF_PRIORITY && !current_is_kswapd()) |
1504 | break; | |
1da177e4 LT |
1505 | } |
1506 | ||
01dbe5c9 KM |
1507 | sc->nr_reclaimed = nr_reclaimed; |
1508 | ||
556adecb RR |
1509 | /* |
1510 | * Even if we did not try to evict anon pages at all, we want to | |
1511 | * rebalance the anon lru active/inactive ratio. | |
1512 | */ | |
14797e23 | 1513 | if (inactive_anon_is_low(zone, sc)) |
556adecb RR |
1514 | shrink_active_list(SWAP_CLUSTER_MAX, zone, sc, priority, 0); |
1515 | ||
232ea4d6 | 1516 | throttle_vm_writeout(sc->gfp_mask); |
1da177e4 LT |
1517 | } |
1518 | ||
1519 | /* | |
1520 | * This is the direct reclaim path, for page-allocating processes. We only | |
1521 | * try to reclaim pages from zones which will satisfy the caller's allocation | |
1522 | * request. | |
1523 | * | |
1524 | * We reclaim from a zone even if that zone is over pages_high. Because: | |
1525 | * a) The caller may be trying to free *extra* pages to satisfy a higher-order | |
1526 | * allocation or | |
1527 | * b) The zones may be over pages_high but they must go *over* pages_high to | |
1528 | * satisfy the `incremental min' zone defense algorithm. | |
1529 | * | |
1da177e4 LT |
1530 | * If a zone is deemed to be full of pinned pages then just give it a light |
1531 | * scan then give up on it. | |
1532 | */ | |
a79311c1 | 1533 | static void shrink_zones(int priority, struct zonelist *zonelist, |
05ff5137 | 1534 | struct scan_control *sc) |
1da177e4 | 1535 | { |
54a6eb5c | 1536 | enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask); |
dd1a239f | 1537 | struct zoneref *z; |
54a6eb5c | 1538 | struct zone *zone; |
1cfb419b | 1539 | |
408d8544 | 1540 | sc->all_unreclaimable = 1; |
54a6eb5c | 1541 | for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) { |
f3fe6512 | 1542 | if (!populated_zone(zone)) |
1da177e4 | 1543 | continue; |
1cfb419b KH |
1544 | /* |
1545 | * Take care memory controller reclaiming has small influence | |
1546 | * to global LRU. | |
1547 | */ | |
e72e2bd6 | 1548 | if (scanning_global_lru(sc)) { |
1cfb419b KH |
1549 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1550 | continue; | |
1551 | note_zone_scanning_priority(zone, priority); | |
1da177e4 | 1552 | |
1cfb419b KH |
1553 | if (zone_is_all_unreclaimable(zone) && |
1554 | priority != DEF_PRIORITY) | |
1555 | continue; /* Let kswapd poll it */ | |
1556 | sc->all_unreclaimable = 0; | |
1557 | } else { | |
1558 | /* | |
1559 | * Ignore cpuset limitation here. We just want to reduce | |
1560 | * # of used pages by us regardless of memory shortage. | |
1561 | */ | |
1562 | sc->all_unreclaimable = 0; | |
1563 | mem_cgroup_note_reclaim_priority(sc->mem_cgroup, | |
1564 | priority); | |
1565 | } | |
408d8544 | 1566 | |
a79311c1 | 1567 | shrink_zone(priority, zone, sc); |
1da177e4 LT |
1568 | } |
1569 | } | |
4f98a2fe | 1570 | |
1da177e4 LT |
1571 | /* |
1572 | * This is the main entry point to direct page reclaim. | |
1573 | * | |
1574 | * If a full scan of the inactive list fails to free enough memory then we | |
1575 | * are "out of memory" and something needs to be killed. | |
1576 | * | |
1577 | * If the caller is !__GFP_FS then the probability of a failure is reasonably | |
1578 | * high - the zone may be full of dirty or under-writeback pages, which this | |
1579 | * caller can't do much about. We kick pdflush and take explicit naps in the | |
1580 | * hope that some of these pages can be written. But if the allocating task | |
1581 | * holds filesystem locks which prevent writeout this might not work, and the | |
1582 | * allocation attempt will fail. | |
a41f24ea NA |
1583 | * |
1584 | * returns: 0, if no pages reclaimed | |
1585 | * else, the number of pages reclaimed | |
1da177e4 | 1586 | */ |
dac1d27b | 1587 | static unsigned long do_try_to_free_pages(struct zonelist *zonelist, |
dd1a239f | 1588 | struct scan_control *sc) |
1da177e4 LT |
1589 | { |
1590 | int priority; | |
c700be3d | 1591 | unsigned long ret = 0; |
69e05944 | 1592 | unsigned long total_scanned = 0; |
1da177e4 | 1593 | struct reclaim_state *reclaim_state = current->reclaim_state; |
1da177e4 | 1594 | unsigned long lru_pages = 0; |
dd1a239f | 1595 | struct zoneref *z; |
54a6eb5c | 1596 | struct zone *zone; |
dd1a239f | 1597 | enum zone_type high_zoneidx = gfp_zone(sc->gfp_mask); |
1da177e4 | 1598 | |
873b4771 KK |
1599 | delayacct_freepages_start(); |
1600 | ||
e72e2bd6 | 1601 | if (scanning_global_lru(sc)) |
1cfb419b KH |
1602 | count_vm_event(ALLOCSTALL); |
1603 | /* | |
1604 | * mem_cgroup will not do shrink_slab. | |
1605 | */ | |
e72e2bd6 | 1606 | if (scanning_global_lru(sc)) { |
54a6eb5c | 1607 | for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) { |
1da177e4 | 1608 | |
1cfb419b KH |
1609 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1610 | continue; | |
1da177e4 | 1611 | |
4f98a2fe | 1612 | lru_pages += zone_lru_pages(zone); |
1cfb419b | 1613 | } |
1da177e4 LT |
1614 | } |
1615 | ||
1616 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { | |
66e1707b | 1617 | sc->nr_scanned = 0; |
f7b7fd8f RR |
1618 | if (!priority) |
1619 | disable_swap_token(); | |
a79311c1 | 1620 | shrink_zones(priority, zonelist, sc); |
66e1707b BS |
1621 | /* |
1622 | * Don't shrink slabs when reclaiming memory from | |
1623 | * over limit cgroups | |
1624 | */ | |
e72e2bd6 | 1625 | if (scanning_global_lru(sc)) { |
dd1a239f | 1626 | shrink_slab(sc->nr_scanned, sc->gfp_mask, lru_pages); |
91a45470 | 1627 | if (reclaim_state) { |
a79311c1 | 1628 | sc->nr_reclaimed += reclaim_state->reclaimed_slab; |
91a45470 KH |
1629 | reclaim_state->reclaimed_slab = 0; |
1630 | } | |
1da177e4 | 1631 | } |
66e1707b | 1632 | total_scanned += sc->nr_scanned; |
a79311c1 RR |
1633 | if (sc->nr_reclaimed >= sc->swap_cluster_max) { |
1634 | ret = sc->nr_reclaimed; | |
1da177e4 LT |
1635 | goto out; |
1636 | } | |
1637 | ||
1638 | /* | |
1639 | * Try to write back as many pages as we just scanned. This | |
1640 | * tends to cause slow streaming writers to write data to the | |
1641 | * disk smoothly, at the dirtying rate, which is nice. But | |
1642 | * that's undesirable in laptop mode, where we *want* lumpy | |
1643 | * writeout. So in laptop mode, write out the whole world. | |
1644 | */ | |
66e1707b BS |
1645 | if (total_scanned > sc->swap_cluster_max + |
1646 | sc->swap_cluster_max / 2) { | |
687a21ce | 1647 | wakeup_pdflush(laptop_mode ? 0 : total_scanned); |
66e1707b | 1648 | sc->may_writepage = 1; |
1da177e4 LT |
1649 | } |
1650 | ||
1651 | /* Take a nap, wait for some writeback to complete */ | |
4dd4b920 | 1652 | if (sc->nr_scanned && priority < DEF_PRIORITY - 2) |
3fcfab16 | 1653 | congestion_wait(WRITE, HZ/10); |
1da177e4 | 1654 | } |
87547ee9 | 1655 | /* top priority shrink_zones still had more to do? don't OOM, then */ |
e72e2bd6 | 1656 | if (!sc->all_unreclaimable && scanning_global_lru(sc)) |
a79311c1 | 1657 | ret = sc->nr_reclaimed; |
1da177e4 | 1658 | out: |
3bb1a852 MB |
1659 | /* |
1660 | * Now that we've scanned all the zones at this priority level, note | |
1661 | * that level within the zone so that the next thread which performs | |
1662 | * scanning of this zone will immediately start out at this priority | |
1663 | * level. This affects only the decision whether or not to bring | |
1664 | * mapped pages onto the inactive list. | |
1665 | */ | |
1666 | if (priority < 0) | |
1667 | priority = 0; | |
1da177e4 | 1668 | |
e72e2bd6 | 1669 | if (scanning_global_lru(sc)) { |
54a6eb5c | 1670 | for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) { |
1cfb419b KH |
1671 | |
1672 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) | |
1673 | continue; | |
1674 | ||
1675 | zone->prev_priority = priority; | |
1676 | } | |
1677 | } else | |
1678 | mem_cgroup_record_reclaim_priority(sc->mem_cgroup, priority); | |
1da177e4 | 1679 | |
873b4771 KK |
1680 | delayacct_freepages_end(); |
1681 | ||
1da177e4 LT |
1682 | return ret; |
1683 | } | |
1684 | ||
dac1d27b MG |
1685 | unsigned long try_to_free_pages(struct zonelist *zonelist, int order, |
1686 | gfp_t gfp_mask) | |
66e1707b BS |
1687 | { |
1688 | struct scan_control sc = { | |
1689 | .gfp_mask = gfp_mask, | |
1690 | .may_writepage = !laptop_mode, | |
1691 | .swap_cluster_max = SWAP_CLUSTER_MAX, | |
a6dc60f8 | 1692 | .may_unmap = 1, |
66e1707b BS |
1693 | .swappiness = vm_swappiness, |
1694 | .order = order, | |
1695 | .mem_cgroup = NULL, | |
1696 | .isolate_pages = isolate_pages_global, | |
1697 | }; | |
1698 | ||
dd1a239f | 1699 | return do_try_to_free_pages(zonelist, &sc); |
66e1707b BS |
1700 | } |
1701 | ||
00f0b825 | 1702 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR |
66e1707b | 1703 | |
e1a1cd59 | 1704 | unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont, |
a7885eb8 KM |
1705 | gfp_t gfp_mask, |
1706 | bool noswap, | |
1707 | unsigned int swappiness) | |
66e1707b BS |
1708 | { |
1709 | struct scan_control sc = { | |
66e1707b | 1710 | .may_writepage = !laptop_mode, |
a6dc60f8 | 1711 | .may_unmap = 1, |
66e1707b | 1712 | .swap_cluster_max = SWAP_CLUSTER_MAX, |
a7885eb8 | 1713 | .swappiness = swappiness, |
66e1707b BS |
1714 | .order = 0, |
1715 | .mem_cgroup = mem_cont, | |
1716 | .isolate_pages = mem_cgroup_isolate_pages, | |
1717 | }; | |
dac1d27b | 1718 | struct zonelist *zonelist; |
66e1707b | 1719 | |
8c7c6e34 | 1720 | if (noswap) |
a6dc60f8 | 1721 | sc.may_unmap = 0; |
8c7c6e34 | 1722 | |
dd1a239f MG |
1723 | sc.gfp_mask = (gfp_mask & GFP_RECLAIM_MASK) | |
1724 | (GFP_HIGHUSER_MOVABLE & ~GFP_RECLAIM_MASK); | |
1725 | zonelist = NODE_DATA(numa_node_id())->node_zonelists; | |
1726 | return do_try_to_free_pages(zonelist, &sc); | |
66e1707b BS |
1727 | } |
1728 | #endif | |
1729 | ||
1da177e4 LT |
1730 | /* |
1731 | * For kswapd, balance_pgdat() will work across all this node's zones until | |
1732 | * they are all at pages_high. | |
1733 | * | |
1da177e4 LT |
1734 | * Returns the number of pages which were actually freed. |
1735 | * | |
1736 | * There is special handling here for zones which are full of pinned pages. | |
1737 | * This can happen if the pages are all mlocked, or if they are all used by | |
1738 | * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. | |
1739 | * What we do is to detect the case where all pages in the zone have been | |
1740 | * scanned twice and there has been zero successful reclaim. Mark the zone as | |
1741 | * dead and from now on, only perform a short scan. Basically we're polling | |
1742 | * the zone for when the problem goes away. | |
1743 | * | |
1744 | * kswapd scans the zones in the highmem->normal->dma direction. It skips | |
1745 | * zones which have free_pages > pages_high, but once a zone is found to have | |
1746 | * free_pages <= pages_high, we scan that zone and the lower zones regardless | |
1747 | * of the number of free pages in the lower zones. This interoperates with | |
1748 | * the page allocator fallback scheme to ensure that aging of pages is balanced | |
1749 | * across the zones. | |
1750 | */ | |
d6277db4 | 1751 | static unsigned long balance_pgdat(pg_data_t *pgdat, int order) |
1da177e4 | 1752 | { |
1da177e4 LT |
1753 | int all_zones_ok; |
1754 | int priority; | |
1755 | int i; | |
69e05944 | 1756 | unsigned long total_scanned; |
1da177e4 | 1757 | struct reclaim_state *reclaim_state = current->reclaim_state; |
179e9639 AM |
1758 | struct scan_control sc = { |
1759 | .gfp_mask = GFP_KERNEL, | |
a6dc60f8 | 1760 | .may_unmap = 1, |
d6277db4 RW |
1761 | .swap_cluster_max = SWAP_CLUSTER_MAX, |
1762 | .swappiness = vm_swappiness, | |
5ad333eb | 1763 | .order = order, |
66e1707b BS |
1764 | .mem_cgroup = NULL, |
1765 | .isolate_pages = isolate_pages_global, | |
179e9639 | 1766 | }; |
3bb1a852 MB |
1767 | /* |
1768 | * temp_priority is used to remember the scanning priority at which | |
1769 | * this zone was successfully refilled to free_pages == pages_high. | |
1770 | */ | |
1771 | int temp_priority[MAX_NR_ZONES]; | |
1da177e4 LT |
1772 | |
1773 | loop_again: | |
1774 | total_scanned = 0; | |
a79311c1 | 1775 | sc.nr_reclaimed = 0; |
c0bbbc73 | 1776 | sc.may_writepage = !laptop_mode; |
f8891e5e | 1777 | count_vm_event(PAGEOUTRUN); |
1da177e4 | 1778 | |
3bb1a852 MB |
1779 | for (i = 0; i < pgdat->nr_zones; i++) |
1780 | temp_priority[i] = DEF_PRIORITY; | |
1da177e4 LT |
1781 | |
1782 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { | |
1783 | int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ | |
1784 | unsigned long lru_pages = 0; | |
1785 | ||
f7b7fd8f RR |
1786 | /* The swap token gets in the way of swapout... */ |
1787 | if (!priority) | |
1788 | disable_swap_token(); | |
1789 | ||
1da177e4 LT |
1790 | all_zones_ok = 1; |
1791 | ||
d6277db4 RW |
1792 | /* |
1793 | * Scan in the highmem->dma direction for the highest | |
1794 | * zone which needs scanning | |
1795 | */ | |
1796 | for (i = pgdat->nr_zones - 1; i >= 0; i--) { | |
1797 | struct zone *zone = pgdat->node_zones + i; | |
1da177e4 | 1798 | |
d6277db4 RW |
1799 | if (!populated_zone(zone)) |
1800 | continue; | |
1da177e4 | 1801 | |
e815af95 DR |
1802 | if (zone_is_all_unreclaimable(zone) && |
1803 | priority != DEF_PRIORITY) | |
d6277db4 | 1804 | continue; |
1da177e4 | 1805 | |
556adecb RR |
1806 | /* |
1807 | * Do some background aging of the anon list, to give | |
1808 | * pages a chance to be referenced before reclaiming. | |
1809 | */ | |
14797e23 | 1810 | if (inactive_anon_is_low(zone, &sc)) |
556adecb RR |
1811 | shrink_active_list(SWAP_CLUSTER_MAX, zone, |
1812 | &sc, priority, 0); | |
1813 | ||
d6277db4 RW |
1814 | if (!zone_watermark_ok(zone, order, zone->pages_high, |
1815 | 0, 0)) { | |
1816 | end_zone = i; | |
e1dbeda6 | 1817 | break; |
1da177e4 | 1818 | } |
1da177e4 | 1819 | } |
e1dbeda6 AM |
1820 | if (i < 0) |
1821 | goto out; | |
1822 | ||
1da177e4 LT |
1823 | for (i = 0; i <= end_zone; i++) { |
1824 | struct zone *zone = pgdat->node_zones + i; | |
1825 | ||
4f98a2fe | 1826 | lru_pages += zone_lru_pages(zone); |
1da177e4 LT |
1827 | } |
1828 | ||
1829 | /* | |
1830 | * Now scan the zone in the dma->highmem direction, stopping | |
1831 | * at the last zone which needs scanning. | |
1832 | * | |
1833 | * We do this because the page allocator works in the opposite | |
1834 | * direction. This prevents the page allocator from allocating | |
1835 | * pages behind kswapd's direction of progress, which would | |
1836 | * cause too much scanning of the lower zones. | |
1837 | */ | |
1838 | for (i = 0; i <= end_zone; i++) { | |
1839 | struct zone *zone = pgdat->node_zones + i; | |
b15e0905 | 1840 | int nr_slab; |
1da177e4 | 1841 | |
f3fe6512 | 1842 | if (!populated_zone(zone)) |
1da177e4 LT |
1843 | continue; |
1844 | ||
e815af95 DR |
1845 | if (zone_is_all_unreclaimable(zone) && |
1846 | priority != DEF_PRIORITY) | |
1da177e4 LT |
1847 | continue; |
1848 | ||
d6277db4 RW |
1849 | if (!zone_watermark_ok(zone, order, zone->pages_high, |
1850 | end_zone, 0)) | |
1851 | all_zones_ok = 0; | |
3bb1a852 | 1852 | temp_priority[i] = priority; |
1da177e4 | 1853 | sc.nr_scanned = 0; |
3bb1a852 | 1854 | note_zone_scanning_priority(zone, priority); |
32a4330d RR |
1855 | /* |
1856 | * We put equal pressure on every zone, unless one | |
1857 | * zone has way too many pages free already. | |
1858 | */ | |
1859 | if (!zone_watermark_ok(zone, order, 8*zone->pages_high, | |
1860 | end_zone, 0)) | |
a79311c1 | 1861 | shrink_zone(priority, zone, &sc); |
1da177e4 | 1862 | reclaim_state->reclaimed_slab = 0; |
b15e0905 | 1863 | nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL, |
1864 | lru_pages); | |
a79311c1 | 1865 | sc.nr_reclaimed += reclaim_state->reclaimed_slab; |
1da177e4 | 1866 | total_scanned += sc.nr_scanned; |
e815af95 | 1867 | if (zone_is_all_unreclaimable(zone)) |
1da177e4 | 1868 | continue; |
b15e0905 | 1869 | if (nr_slab == 0 && zone->pages_scanned >= |
4f98a2fe | 1870 | (zone_lru_pages(zone) * 6)) |
e815af95 DR |
1871 | zone_set_flag(zone, |
1872 | ZONE_ALL_UNRECLAIMABLE); | |
1da177e4 LT |
1873 | /* |
1874 | * If we've done a decent amount of scanning and | |
1875 | * the reclaim ratio is low, start doing writepage | |
1876 | * even in laptop mode | |
1877 | */ | |
1878 | if (total_scanned > SWAP_CLUSTER_MAX * 2 && | |
a79311c1 | 1879 | total_scanned > sc.nr_reclaimed + sc.nr_reclaimed / 2) |
1da177e4 LT |
1880 | sc.may_writepage = 1; |
1881 | } | |
1da177e4 LT |
1882 | if (all_zones_ok) |
1883 | break; /* kswapd: all done */ | |
1884 | /* | |
1885 | * OK, kswapd is getting into trouble. Take a nap, then take | |
1886 | * another pass across the zones. | |
1887 | */ | |
4dd4b920 | 1888 | if (total_scanned && priority < DEF_PRIORITY - 2) |
3fcfab16 | 1889 | congestion_wait(WRITE, HZ/10); |
1da177e4 LT |
1890 | |
1891 | /* | |
1892 | * We do this so kswapd doesn't build up large priorities for | |
1893 | * example when it is freeing in parallel with allocators. It | |
1894 | * matches the direct reclaim path behaviour in terms of impact | |
1895 | * on zone->*_priority. | |
1896 | */ | |
a79311c1 | 1897 | if (sc.nr_reclaimed >= SWAP_CLUSTER_MAX) |
1da177e4 LT |
1898 | break; |
1899 | } | |
1900 | out: | |
3bb1a852 MB |
1901 | /* |
1902 | * Note within each zone the priority level at which this zone was | |
1903 | * brought into a happy state. So that the next thread which scans this | |
1904 | * zone will start out at that priority level. | |
1905 | */ | |
1da177e4 LT |
1906 | for (i = 0; i < pgdat->nr_zones; i++) { |
1907 | struct zone *zone = pgdat->node_zones + i; | |
1908 | ||
3bb1a852 | 1909 | zone->prev_priority = temp_priority[i]; |
1da177e4 LT |
1910 | } |
1911 | if (!all_zones_ok) { | |
1912 | cond_resched(); | |
8357376d RW |
1913 | |
1914 | try_to_freeze(); | |
1915 | ||
73ce02e9 KM |
1916 | /* |
1917 | * Fragmentation may mean that the system cannot be | |
1918 | * rebalanced for high-order allocations in all zones. | |
1919 | * At this point, if nr_reclaimed < SWAP_CLUSTER_MAX, | |
1920 | * it means the zones have been fully scanned and are still | |
1921 | * not balanced. For high-order allocations, there is | |
1922 | * little point trying all over again as kswapd may | |
1923 | * infinite loop. | |
1924 | * | |
1925 | * Instead, recheck all watermarks at order-0 as they | |
1926 | * are the most important. If watermarks are ok, kswapd will go | |
1927 | * back to sleep. High-order users can still perform direct | |
1928 | * reclaim if they wish. | |
1929 | */ | |
1930 | if (sc.nr_reclaimed < SWAP_CLUSTER_MAX) | |
1931 | order = sc.order = 0; | |
1932 | ||
1da177e4 LT |
1933 | goto loop_again; |
1934 | } | |
1935 | ||
a79311c1 | 1936 | return sc.nr_reclaimed; |
1da177e4 LT |
1937 | } |
1938 | ||
1939 | /* | |
1940 | * The background pageout daemon, started as a kernel thread | |
4f98a2fe | 1941 | * from the init process. |
1da177e4 LT |
1942 | * |
1943 | * This basically trickles out pages so that we have _some_ | |
1944 | * free memory available even if there is no other activity | |
1945 | * that frees anything up. This is needed for things like routing | |
1946 | * etc, where we otherwise might have all activity going on in | |
1947 | * asynchronous contexts that cannot page things out. | |
1948 | * | |
1949 | * If there are applications that are active memory-allocators | |
1950 | * (most normal use), this basically shouldn't matter. | |
1951 | */ | |
1952 | static int kswapd(void *p) | |
1953 | { | |
1954 | unsigned long order; | |
1955 | pg_data_t *pgdat = (pg_data_t*)p; | |
1956 | struct task_struct *tsk = current; | |
1957 | DEFINE_WAIT(wait); | |
1958 | struct reclaim_state reclaim_state = { | |
1959 | .reclaimed_slab = 0, | |
1960 | }; | |
c5f59f08 | 1961 | node_to_cpumask_ptr(cpumask, pgdat->node_id); |
1da177e4 | 1962 | |
cf40bd16 NP |
1963 | lockdep_set_current_reclaim_state(GFP_KERNEL); |
1964 | ||
174596a0 | 1965 | if (!cpumask_empty(cpumask)) |
c5f59f08 | 1966 | set_cpus_allowed_ptr(tsk, cpumask); |
1da177e4 LT |
1967 | current->reclaim_state = &reclaim_state; |
1968 | ||
1969 | /* | |
1970 | * Tell the memory management that we're a "memory allocator", | |
1971 | * and that if we need more memory we should get access to it | |
1972 | * regardless (see "__alloc_pages()"). "kswapd" should | |
1973 | * never get caught in the normal page freeing logic. | |
1974 | * | |
1975 | * (Kswapd normally doesn't need memory anyway, but sometimes | |
1976 | * you need a small amount of memory in order to be able to | |
1977 | * page out something else, and this flag essentially protects | |
1978 | * us from recursively trying to free more memory as we're | |
1979 | * trying to free the first piece of memory in the first place). | |
1980 | */ | |
930d9152 | 1981 | tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; |
83144186 | 1982 | set_freezable(); |
1da177e4 LT |
1983 | |
1984 | order = 0; | |
1985 | for ( ; ; ) { | |
1986 | unsigned long new_order; | |
3e1d1d28 | 1987 | |
1da177e4 LT |
1988 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); |
1989 | new_order = pgdat->kswapd_max_order; | |
1990 | pgdat->kswapd_max_order = 0; | |
1991 | if (order < new_order) { | |
1992 | /* | |
1993 | * Don't sleep if someone wants a larger 'order' | |
1994 | * allocation | |
1995 | */ | |
1996 | order = new_order; | |
1997 | } else { | |
b1296cc4 RW |
1998 | if (!freezing(current)) |
1999 | schedule(); | |
2000 | ||
1da177e4 LT |
2001 | order = pgdat->kswapd_max_order; |
2002 | } | |
2003 | finish_wait(&pgdat->kswapd_wait, &wait); | |
2004 | ||
b1296cc4 RW |
2005 | if (!try_to_freeze()) { |
2006 | /* We can speed up thawing tasks if we don't call | |
2007 | * balance_pgdat after returning from the refrigerator | |
2008 | */ | |
2009 | balance_pgdat(pgdat, order); | |
2010 | } | |
1da177e4 LT |
2011 | } |
2012 | return 0; | |
2013 | } | |
2014 | ||
2015 | /* | |
2016 | * A zone is low on free memory, so wake its kswapd task to service it. | |
2017 | */ | |
2018 | void wakeup_kswapd(struct zone *zone, int order) | |
2019 | { | |
2020 | pg_data_t *pgdat; | |
2021 | ||
f3fe6512 | 2022 | if (!populated_zone(zone)) |
1da177e4 LT |
2023 | return; |
2024 | ||
2025 | pgdat = zone->zone_pgdat; | |
7fb1d9fc | 2026 | if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0)) |
1da177e4 LT |
2027 | return; |
2028 | if (pgdat->kswapd_max_order < order) | |
2029 | pgdat->kswapd_max_order = order; | |
02a0e53d | 2030 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1da177e4 | 2031 | return; |
8d0986e2 | 2032 | if (!waitqueue_active(&pgdat->kswapd_wait)) |
1da177e4 | 2033 | return; |
8d0986e2 | 2034 | wake_up_interruptible(&pgdat->kswapd_wait); |
1da177e4 LT |
2035 | } |
2036 | ||
4f98a2fe RR |
2037 | unsigned long global_lru_pages(void) |
2038 | { | |
2039 | return global_page_state(NR_ACTIVE_ANON) | |
2040 | + global_page_state(NR_ACTIVE_FILE) | |
2041 | + global_page_state(NR_INACTIVE_ANON) | |
2042 | + global_page_state(NR_INACTIVE_FILE); | |
2043 | } | |
2044 | ||
1da177e4 LT |
2045 | #ifdef CONFIG_PM |
2046 | /* | |
d6277db4 | 2047 | * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages |
d979677c | 2048 | * from LRU lists system-wide, for given pass and priority. |
d6277db4 RW |
2049 | * |
2050 | * For pass > 3 we also try to shrink the LRU lists that contain a few pages | |
2051 | */ | |
d979677c | 2052 | static void shrink_all_zones(unsigned long nr_pages, int prio, |
e07aa05b | 2053 | int pass, struct scan_control *sc) |
d6277db4 RW |
2054 | { |
2055 | struct zone *zone; | |
d979677c | 2056 | unsigned long nr_reclaimed = 0; |
d6277db4 | 2057 | |
ee99c71c | 2058 | for_each_populated_zone(zone) { |
0cb57258 | 2059 | enum lru_list l; |
d6277db4 | 2060 | |
e815af95 | 2061 | if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY) |
d6277db4 RW |
2062 | continue; |
2063 | ||
894bc310 | 2064 | for_each_evictable_lru(l) { |
0cb57258 JW |
2065 | enum zone_stat_item ls = NR_LRU_BASE + l; |
2066 | unsigned long lru_pages = zone_page_state(zone, ls); | |
2067 | ||
894bc310 | 2068 | /* For pass = 0, we don't shrink the active list */ |
0cb57258 JW |
2069 | if (pass == 0 && (l == LRU_ACTIVE_ANON || |
2070 | l == LRU_ACTIVE_FILE)) | |
b69408e8 CL |
2071 | continue; |
2072 | ||
0cb57258 | 2073 | zone->lru[l].nr_scan += (lru_pages >> prio) + 1; |
b69408e8 | 2074 | if (zone->lru[l].nr_scan >= nr_pages || pass > 3) { |
0cb57258 JW |
2075 | unsigned long nr_to_scan; |
2076 | ||
b69408e8 | 2077 | zone->lru[l].nr_scan = 0; |
0cb57258 | 2078 | nr_to_scan = min(nr_pages, lru_pages); |
d979677c | 2079 | nr_reclaimed += shrink_list(l, nr_to_scan, zone, |
b69408e8 | 2080 | sc, prio); |
d979677c MK |
2081 | if (nr_reclaimed >= nr_pages) { |
2082 | sc->nr_reclaimed = nr_reclaimed; | |
2083 | return; | |
2084 | } | |
d6277db4 RW |
2085 | } |
2086 | } | |
d6277db4 | 2087 | } |
d979677c | 2088 | sc->nr_reclaimed = nr_reclaimed; |
d6277db4 RW |
2089 | } |
2090 | ||
2091 | /* | |
2092 | * Try to free `nr_pages' of memory, system-wide, and return the number of | |
2093 | * freed pages. | |
2094 | * | |
2095 | * Rather than trying to age LRUs the aim is to preserve the overall | |
2096 | * LRU order by reclaiming preferentially | |
2097 | * inactive > active > active referenced > active mapped | |
1da177e4 | 2098 | */ |
69e05944 | 2099 | unsigned long shrink_all_memory(unsigned long nr_pages) |
1da177e4 | 2100 | { |
d6277db4 | 2101 | unsigned long lru_pages, nr_slab; |
d6277db4 RW |
2102 | int pass; |
2103 | struct reclaim_state reclaim_state; | |
d6277db4 RW |
2104 | struct scan_control sc = { |
2105 | .gfp_mask = GFP_KERNEL, | |
a6dc60f8 | 2106 | .may_unmap = 0, |
d6277db4 | 2107 | .may_writepage = 1, |
66e1707b | 2108 | .isolate_pages = isolate_pages_global, |
1da177e4 LT |
2109 | }; |
2110 | ||
2111 | current->reclaim_state = &reclaim_state; | |
69e05944 | 2112 | |
4f98a2fe | 2113 | lru_pages = global_lru_pages(); |
972d1a7b | 2114 | nr_slab = global_page_state(NR_SLAB_RECLAIMABLE); |
d6277db4 RW |
2115 | /* If slab caches are huge, it's better to hit them first */ |
2116 | while (nr_slab >= lru_pages) { | |
2117 | reclaim_state.reclaimed_slab = 0; | |
2118 | shrink_slab(nr_pages, sc.gfp_mask, lru_pages); | |
2119 | if (!reclaim_state.reclaimed_slab) | |
1da177e4 | 2120 | break; |
d6277db4 | 2121 | |
d979677c MK |
2122 | sc.nr_reclaimed += reclaim_state.reclaimed_slab; |
2123 | if (sc.nr_reclaimed >= nr_pages) | |
d6277db4 RW |
2124 | goto out; |
2125 | ||
2126 | nr_slab -= reclaim_state.reclaimed_slab; | |
1da177e4 | 2127 | } |
d6277db4 RW |
2128 | |
2129 | /* | |
2130 | * We try to shrink LRUs in 5 passes: | |
2131 | * 0 = Reclaim from inactive_list only | |
2132 | * 1 = Reclaim from active list but don't reclaim mapped | |
2133 | * 2 = 2nd pass of type 1 | |
2134 | * 3 = Reclaim mapped (normal reclaim) | |
2135 | * 4 = 2nd pass of type 3 | |
2136 | */ | |
2137 | for (pass = 0; pass < 5; pass++) { | |
2138 | int prio; | |
2139 | ||
d6277db4 | 2140 | /* Force reclaiming mapped pages in the passes #3 and #4 */ |
3049103d | 2141 | if (pass > 2) |
a6dc60f8 | 2142 | sc.may_unmap = 1; |
d6277db4 RW |
2143 | |
2144 | for (prio = DEF_PRIORITY; prio >= 0; prio--) { | |
d979677c | 2145 | unsigned long nr_to_scan = nr_pages - sc.nr_reclaimed; |
d6277db4 | 2146 | |
d6277db4 | 2147 | sc.nr_scanned = 0; |
9786bf84 | 2148 | sc.swap_cluster_max = nr_to_scan; |
d979677c MK |
2149 | shrink_all_zones(nr_to_scan, prio, pass, &sc); |
2150 | if (sc.nr_reclaimed >= nr_pages) | |
d6277db4 RW |
2151 | goto out; |
2152 | ||
2153 | reclaim_state.reclaimed_slab = 0; | |
76395d37 | 2154 | shrink_slab(sc.nr_scanned, sc.gfp_mask, |
4f98a2fe | 2155 | global_lru_pages()); |
d979677c MK |
2156 | sc.nr_reclaimed += reclaim_state.reclaimed_slab; |
2157 | if (sc.nr_reclaimed >= nr_pages) | |
d6277db4 RW |
2158 | goto out; |
2159 | ||
2160 | if (sc.nr_scanned && prio < DEF_PRIORITY - 2) | |
3fcfab16 | 2161 | congestion_wait(WRITE, HZ / 10); |
d6277db4 | 2162 | } |
248a0301 | 2163 | } |
d6277db4 RW |
2164 | |
2165 | /* | |
d979677c MK |
2166 | * If sc.nr_reclaimed = 0, we could not shrink LRUs, but there may be |
2167 | * something in slab caches | |
d6277db4 | 2168 | */ |
d979677c | 2169 | if (!sc.nr_reclaimed) { |
d6277db4 RW |
2170 | do { |
2171 | reclaim_state.reclaimed_slab = 0; | |
4f98a2fe | 2172 | shrink_slab(nr_pages, sc.gfp_mask, global_lru_pages()); |
d979677c MK |
2173 | sc.nr_reclaimed += reclaim_state.reclaimed_slab; |
2174 | } while (sc.nr_reclaimed < nr_pages && | |
2175 | reclaim_state.reclaimed_slab > 0); | |
76395d37 | 2176 | } |
d6277db4 | 2177 | |
d979677c | 2178 | |
d6277db4 | 2179 | out: |
1da177e4 | 2180 | current->reclaim_state = NULL; |
d6277db4 | 2181 | |
d979677c | 2182 | return sc.nr_reclaimed; |
1da177e4 LT |
2183 | } |
2184 | #endif | |
2185 | ||
1da177e4 LT |
2186 | /* It's optimal to keep kswapds on the same CPUs as their memory, but |
2187 | not required for correctness. So if the last cpu in a node goes | |
2188 | away, we get changed to run anywhere: as the first one comes back, | |
2189 | restore their cpu bindings. */ | |
9c7b216d | 2190 | static int __devinit cpu_callback(struct notifier_block *nfb, |
69e05944 | 2191 | unsigned long action, void *hcpu) |
1da177e4 | 2192 | { |
58c0a4a7 | 2193 | int nid; |
1da177e4 | 2194 | |
8bb78442 | 2195 | if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { |
58c0a4a7 | 2196 | for_each_node_state(nid, N_HIGH_MEMORY) { |
c5f59f08 MT |
2197 | pg_data_t *pgdat = NODE_DATA(nid); |
2198 | node_to_cpumask_ptr(mask, pgdat->node_id); | |
2199 | ||
3e597945 | 2200 | if (cpumask_any_and(cpu_online_mask, mask) < nr_cpu_ids) |
1da177e4 | 2201 | /* One of our CPUs online: restore mask */ |
c5f59f08 | 2202 | set_cpus_allowed_ptr(pgdat->kswapd, mask); |
1da177e4 LT |
2203 | } |
2204 | } | |
2205 | return NOTIFY_OK; | |
2206 | } | |
1da177e4 | 2207 | |
3218ae14 YG |
2208 | /* |
2209 | * This kswapd start function will be called by init and node-hot-add. | |
2210 | * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. | |
2211 | */ | |
2212 | int kswapd_run(int nid) | |
2213 | { | |
2214 | pg_data_t *pgdat = NODE_DATA(nid); | |
2215 | int ret = 0; | |
2216 | ||
2217 | if (pgdat->kswapd) | |
2218 | return 0; | |
2219 | ||
2220 | pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); | |
2221 | if (IS_ERR(pgdat->kswapd)) { | |
2222 | /* failure at boot is fatal */ | |
2223 | BUG_ON(system_state == SYSTEM_BOOTING); | |
2224 | printk("Failed to start kswapd on node %d\n",nid); | |
2225 | ret = -1; | |
2226 | } | |
2227 | return ret; | |
2228 | } | |
2229 | ||
1da177e4 LT |
2230 | static int __init kswapd_init(void) |
2231 | { | |
3218ae14 | 2232 | int nid; |
69e05944 | 2233 | |
1da177e4 | 2234 | swap_setup(); |
9422ffba | 2235 | for_each_node_state(nid, N_HIGH_MEMORY) |
3218ae14 | 2236 | kswapd_run(nid); |
1da177e4 LT |
2237 | hotcpu_notifier(cpu_callback, 0); |
2238 | return 0; | |
2239 | } | |
2240 | ||
2241 | module_init(kswapd_init) | |
9eeff239 CL |
2242 | |
2243 | #ifdef CONFIG_NUMA | |
2244 | /* | |
2245 | * Zone reclaim mode | |
2246 | * | |
2247 | * If non-zero call zone_reclaim when the number of free pages falls below | |
2248 | * the watermarks. | |
9eeff239 CL |
2249 | */ |
2250 | int zone_reclaim_mode __read_mostly; | |
2251 | ||
1b2ffb78 | 2252 | #define RECLAIM_OFF 0 |
7d03431c | 2253 | #define RECLAIM_ZONE (1<<0) /* Run shrink_inactive_list on the zone */ |
1b2ffb78 CL |
2254 | #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ |
2255 | #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ | |
2256 | ||
a92f7126 CL |
2257 | /* |
2258 | * Priority for ZONE_RECLAIM. This determines the fraction of pages | |
2259 | * of a node considered for each zone_reclaim. 4 scans 1/16th of | |
2260 | * a zone. | |
2261 | */ | |
2262 | #define ZONE_RECLAIM_PRIORITY 4 | |
2263 | ||
9614634f CL |
2264 | /* |
2265 | * Percentage of pages in a zone that must be unmapped for zone_reclaim to | |
2266 | * occur. | |
2267 | */ | |
2268 | int sysctl_min_unmapped_ratio = 1; | |
2269 | ||
0ff38490 CL |
2270 | /* |
2271 | * If the number of slab pages in a zone grows beyond this percentage then | |
2272 | * slab reclaim needs to occur. | |
2273 | */ | |
2274 | int sysctl_min_slab_ratio = 5; | |
2275 | ||
9eeff239 CL |
2276 | /* |
2277 | * Try to free up some pages from this zone through reclaim. | |
2278 | */ | |
179e9639 | 2279 | static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) |
9eeff239 | 2280 | { |
7fb2d46d | 2281 | /* Minimum pages needed in order to stay on node */ |
69e05944 | 2282 | const unsigned long nr_pages = 1 << order; |
9eeff239 CL |
2283 | struct task_struct *p = current; |
2284 | struct reclaim_state reclaim_state; | |
8695949a | 2285 | int priority; |
179e9639 AM |
2286 | struct scan_control sc = { |
2287 | .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), | |
a6dc60f8 | 2288 | .may_unmap = !!(zone_reclaim_mode & RECLAIM_SWAP), |
69e05944 AM |
2289 | .swap_cluster_max = max_t(unsigned long, nr_pages, |
2290 | SWAP_CLUSTER_MAX), | |
179e9639 | 2291 | .gfp_mask = gfp_mask, |
d6277db4 | 2292 | .swappiness = vm_swappiness, |
bd2f6199 | 2293 | .order = order, |
66e1707b | 2294 | .isolate_pages = isolate_pages_global, |
179e9639 | 2295 | }; |
83e33a47 | 2296 | unsigned long slab_reclaimable; |
9eeff239 CL |
2297 | |
2298 | disable_swap_token(); | |
9eeff239 | 2299 | cond_resched(); |
d4f7796e CL |
2300 | /* |
2301 | * We need to be able to allocate from the reserves for RECLAIM_SWAP | |
2302 | * and we also need to be able to write out pages for RECLAIM_WRITE | |
2303 | * and RECLAIM_SWAP. | |
2304 | */ | |
2305 | p->flags |= PF_MEMALLOC | PF_SWAPWRITE; | |
9eeff239 CL |
2306 | reclaim_state.reclaimed_slab = 0; |
2307 | p->reclaim_state = &reclaim_state; | |
c84db23c | 2308 | |
0ff38490 CL |
2309 | if (zone_page_state(zone, NR_FILE_PAGES) - |
2310 | zone_page_state(zone, NR_FILE_MAPPED) > | |
2311 | zone->min_unmapped_pages) { | |
2312 | /* | |
2313 | * Free memory by calling shrink zone with increasing | |
2314 | * priorities until we have enough memory freed. | |
2315 | */ | |
2316 | priority = ZONE_RECLAIM_PRIORITY; | |
2317 | do { | |
3bb1a852 | 2318 | note_zone_scanning_priority(zone, priority); |
a79311c1 | 2319 | shrink_zone(priority, zone, &sc); |
0ff38490 | 2320 | priority--; |
a79311c1 | 2321 | } while (priority >= 0 && sc.nr_reclaimed < nr_pages); |
0ff38490 | 2322 | } |
c84db23c | 2323 | |
83e33a47 CL |
2324 | slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE); |
2325 | if (slab_reclaimable > zone->min_slab_pages) { | |
2a16e3f4 | 2326 | /* |
7fb2d46d | 2327 | * shrink_slab() does not currently allow us to determine how |
0ff38490 CL |
2328 | * many pages were freed in this zone. So we take the current |
2329 | * number of slab pages and shake the slab until it is reduced | |
2330 | * by the same nr_pages that we used for reclaiming unmapped | |
2331 | * pages. | |
2a16e3f4 | 2332 | * |
0ff38490 CL |
2333 | * Note that shrink_slab will free memory on all zones and may |
2334 | * take a long time. | |
2a16e3f4 | 2335 | */ |
0ff38490 | 2336 | while (shrink_slab(sc.nr_scanned, gfp_mask, order) && |
83e33a47 CL |
2337 | zone_page_state(zone, NR_SLAB_RECLAIMABLE) > |
2338 | slab_reclaimable - nr_pages) | |
0ff38490 | 2339 | ; |
83e33a47 CL |
2340 | |
2341 | /* | |
2342 | * Update nr_reclaimed by the number of slab pages we | |
2343 | * reclaimed from this zone. | |
2344 | */ | |
a79311c1 | 2345 | sc.nr_reclaimed += slab_reclaimable - |
83e33a47 | 2346 | zone_page_state(zone, NR_SLAB_RECLAIMABLE); |
2a16e3f4 CL |
2347 | } |
2348 | ||
9eeff239 | 2349 | p->reclaim_state = NULL; |
d4f7796e | 2350 | current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); |
a79311c1 | 2351 | return sc.nr_reclaimed >= nr_pages; |
9eeff239 | 2352 | } |
179e9639 AM |
2353 | |
2354 | int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) | |
2355 | { | |
179e9639 | 2356 | int node_id; |
d773ed6b | 2357 | int ret; |
179e9639 AM |
2358 | |
2359 | /* | |
0ff38490 CL |
2360 | * Zone reclaim reclaims unmapped file backed pages and |
2361 | * slab pages if we are over the defined limits. | |
34aa1330 | 2362 | * |
9614634f CL |
2363 | * A small portion of unmapped file backed pages is needed for |
2364 | * file I/O otherwise pages read by file I/O will be immediately | |
2365 | * thrown out if the zone is overallocated. So we do not reclaim | |
2366 | * if less than a specified percentage of the zone is used by | |
2367 | * unmapped file backed pages. | |
179e9639 | 2368 | */ |
34aa1330 | 2369 | if (zone_page_state(zone, NR_FILE_PAGES) - |
0ff38490 CL |
2370 | zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages |
2371 | && zone_page_state(zone, NR_SLAB_RECLAIMABLE) | |
2372 | <= zone->min_slab_pages) | |
9614634f | 2373 | return 0; |
179e9639 | 2374 | |
d773ed6b DR |
2375 | if (zone_is_all_unreclaimable(zone)) |
2376 | return 0; | |
2377 | ||
179e9639 | 2378 | /* |
d773ed6b | 2379 | * Do not scan if the allocation should not be delayed. |
179e9639 | 2380 | */ |
d773ed6b | 2381 | if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) |
179e9639 AM |
2382 | return 0; |
2383 | ||
2384 | /* | |
2385 | * Only run zone reclaim on the local zone or on zones that do not | |
2386 | * have associated processors. This will favor the local processor | |
2387 | * over remote processors and spread off node memory allocations | |
2388 | * as wide as possible. | |
2389 | */ | |
89fa3024 | 2390 | node_id = zone_to_nid(zone); |
37c0708d | 2391 | if (node_state(node_id, N_CPU) && node_id != numa_node_id()) |
179e9639 | 2392 | return 0; |
d773ed6b DR |
2393 | |
2394 | if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) | |
2395 | return 0; | |
2396 | ret = __zone_reclaim(zone, gfp_mask, order); | |
2397 | zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); | |
2398 | ||
2399 | return ret; | |
179e9639 | 2400 | } |
9eeff239 | 2401 | #endif |
894bc310 LS |
2402 | |
2403 | #ifdef CONFIG_UNEVICTABLE_LRU | |
2404 | /* | |
2405 | * page_evictable - test whether a page is evictable | |
2406 | * @page: the page to test | |
2407 | * @vma: the VMA in which the page is or will be mapped, may be NULL | |
2408 | * | |
2409 | * Test whether page is evictable--i.e., should be placed on active/inactive | |
b291f000 NP |
2410 | * lists vs unevictable list. The vma argument is !NULL when called from the |
2411 | * fault path to determine how to instantate a new page. | |
894bc310 LS |
2412 | * |
2413 | * Reasons page might not be evictable: | |
ba9ddf49 | 2414 | * (1) page's mapping marked unevictable |
b291f000 | 2415 | * (2) page is part of an mlocked VMA |
ba9ddf49 | 2416 | * |
894bc310 LS |
2417 | */ |
2418 | int page_evictable(struct page *page, struct vm_area_struct *vma) | |
2419 | { | |
2420 | ||
ba9ddf49 LS |
2421 | if (mapping_unevictable(page_mapping(page))) |
2422 | return 0; | |
2423 | ||
b291f000 NP |
2424 | if (PageMlocked(page) || (vma && is_mlocked_vma(vma, page))) |
2425 | return 0; | |
894bc310 LS |
2426 | |
2427 | return 1; | |
2428 | } | |
89e004ea LS |
2429 | |
2430 | /** | |
2431 | * check_move_unevictable_page - check page for evictability and move to appropriate zone lru list | |
2432 | * @page: page to check evictability and move to appropriate lru list | |
2433 | * @zone: zone page is in | |
2434 | * | |
2435 | * Checks a page for evictability and moves the page to the appropriate | |
2436 | * zone lru list. | |
2437 | * | |
2438 | * Restrictions: zone->lru_lock must be held, page must be on LRU and must | |
2439 | * have PageUnevictable set. | |
2440 | */ | |
2441 | static void check_move_unevictable_page(struct page *page, struct zone *zone) | |
2442 | { | |
2443 | VM_BUG_ON(PageActive(page)); | |
2444 | ||
2445 | retry: | |
2446 | ClearPageUnevictable(page); | |
2447 | if (page_evictable(page, NULL)) { | |
2448 | enum lru_list l = LRU_INACTIVE_ANON + page_is_file_cache(page); | |
af936a16 | 2449 | |
89e004ea LS |
2450 | __dec_zone_state(zone, NR_UNEVICTABLE); |
2451 | list_move(&page->lru, &zone->lru[l].list); | |
08e552c6 | 2452 | mem_cgroup_move_lists(page, LRU_UNEVICTABLE, l); |
89e004ea LS |
2453 | __inc_zone_state(zone, NR_INACTIVE_ANON + l); |
2454 | __count_vm_event(UNEVICTABLE_PGRESCUED); | |
2455 | } else { | |
2456 | /* | |
2457 | * rotate unevictable list | |
2458 | */ | |
2459 | SetPageUnevictable(page); | |
2460 | list_move(&page->lru, &zone->lru[LRU_UNEVICTABLE].list); | |
08e552c6 | 2461 | mem_cgroup_rotate_lru_list(page, LRU_UNEVICTABLE); |
89e004ea LS |
2462 | if (page_evictable(page, NULL)) |
2463 | goto retry; | |
2464 | } | |
2465 | } | |
2466 | ||
2467 | /** | |
2468 | * scan_mapping_unevictable_pages - scan an address space for evictable pages | |
2469 | * @mapping: struct address_space to scan for evictable pages | |
2470 | * | |
2471 | * Scan all pages in mapping. Check unevictable pages for | |
2472 | * evictability and move them to the appropriate zone lru list. | |
2473 | */ | |
2474 | void scan_mapping_unevictable_pages(struct address_space *mapping) | |
2475 | { | |
2476 | pgoff_t next = 0; | |
2477 | pgoff_t end = (i_size_read(mapping->host) + PAGE_CACHE_SIZE - 1) >> | |
2478 | PAGE_CACHE_SHIFT; | |
2479 | struct zone *zone; | |
2480 | struct pagevec pvec; | |
2481 | ||
2482 | if (mapping->nrpages == 0) | |
2483 | return; | |
2484 | ||
2485 | pagevec_init(&pvec, 0); | |
2486 | while (next < end && | |
2487 | pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) { | |
2488 | int i; | |
2489 | int pg_scanned = 0; | |
2490 | ||
2491 | zone = NULL; | |
2492 | ||
2493 | for (i = 0; i < pagevec_count(&pvec); i++) { | |
2494 | struct page *page = pvec.pages[i]; | |
2495 | pgoff_t page_index = page->index; | |
2496 | struct zone *pagezone = page_zone(page); | |
2497 | ||
2498 | pg_scanned++; | |
2499 | if (page_index > next) | |
2500 | next = page_index; | |
2501 | next++; | |
2502 | ||
2503 | if (pagezone != zone) { | |
2504 | if (zone) | |
2505 | spin_unlock_irq(&zone->lru_lock); | |
2506 | zone = pagezone; | |
2507 | spin_lock_irq(&zone->lru_lock); | |
2508 | } | |
2509 | ||
2510 | if (PageLRU(page) && PageUnevictable(page)) | |
2511 | check_move_unevictable_page(page, zone); | |
2512 | } | |
2513 | if (zone) | |
2514 | spin_unlock_irq(&zone->lru_lock); | |
2515 | pagevec_release(&pvec); | |
2516 | ||
2517 | count_vm_events(UNEVICTABLE_PGSCANNED, pg_scanned); | |
2518 | } | |
2519 | ||
2520 | } | |
af936a16 LS |
2521 | |
2522 | /** | |
2523 | * scan_zone_unevictable_pages - check unevictable list for evictable pages | |
2524 | * @zone - zone of which to scan the unevictable list | |
2525 | * | |
2526 | * Scan @zone's unevictable LRU lists to check for pages that have become | |
2527 | * evictable. Move those that have to @zone's inactive list where they | |
2528 | * become candidates for reclaim, unless shrink_inactive_zone() decides | |
2529 | * to reactivate them. Pages that are still unevictable are rotated | |
2530 | * back onto @zone's unevictable list. | |
2531 | */ | |
2532 | #define SCAN_UNEVICTABLE_BATCH_SIZE 16UL /* arbitrary lock hold batch size */ | |
14b90b22 | 2533 | static void scan_zone_unevictable_pages(struct zone *zone) |
af936a16 LS |
2534 | { |
2535 | struct list_head *l_unevictable = &zone->lru[LRU_UNEVICTABLE].list; | |
2536 | unsigned long scan; | |
2537 | unsigned long nr_to_scan = zone_page_state(zone, NR_UNEVICTABLE); | |
2538 | ||
2539 | while (nr_to_scan > 0) { | |
2540 | unsigned long batch_size = min(nr_to_scan, | |
2541 | SCAN_UNEVICTABLE_BATCH_SIZE); | |
2542 | ||
2543 | spin_lock_irq(&zone->lru_lock); | |
2544 | for (scan = 0; scan < batch_size; scan++) { | |
2545 | struct page *page = lru_to_page(l_unevictable); | |
2546 | ||
2547 | if (!trylock_page(page)) | |
2548 | continue; | |
2549 | ||
2550 | prefetchw_prev_lru_page(page, l_unevictable, flags); | |
2551 | ||
2552 | if (likely(PageLRU(page) && PageUnevictable(page))) | |
2553 | check_move_unevictable_page(page, zone); | |
2554 | ||
2555 | unlock_page(page); | |
2556 | } | |
2557 | spin_unlock_irq(&zone->lru_lock); | |
2558 | ||
2559 | nr_to_scan -= batch_size; | |
2560 | } | |
2561 | } | |
2562 | ||
2563 | ||
2564 | /** | |
2565 | * scan_all_zones_unevictable_pages - scan all unevictable lists for evictable pages | |
2566 | * | |
2567 | * A really big hammer: scan all zones' unevictable LRU lists to check for | |
2568 | * pages that have become evictable. Move those back to the zones' | |
2569 | * inactive list where they become candidates for reclaim. | |
2570 | * This occurs when, e.g., we have unswappable pages on the unevictable lists, | |
2571 | * and we add swap to the system. As such, it runs in the context of a task | |
2572 | * that has possibly/probably made some previously unevictable pages | |
2573 | * evictable. | |
2574 | */ | |
ff30153b | 2575 | static void scan_all_zones_unevictable_pages(void) |
af936a16 LS |
2576 | { |
2577 | struct zone *zone; | |
2578 | ||
2579 | for_each_zone(zone) { | |
2580 | scan_zone_unevictable_pages(zone); | |
2581 | } | |
2582 | } | |
2583 | ||
2584 | /* | |
2585 | * scan_unevictable_pages [vm] sysctl handler. On demand re-scan of | |
2586 | * all nodes' unevictable lists for evictable pages | |
2587 | */ | |
2588 | unsigned long scan_unevictable_pages; | |
2589 | ||
2590 | int scan_unevictable_handler(struct ctl_table *table, int write, | |
2591 | struct file *file, void __user *buffer, | |
2592 | size_t *length, loff_t *ppos) | |
2593 | { | |
2594 | proc_doulongvec_minmax(table, write, file, buffer, length, ppos); | |
2595 | ||
2596 | if (write && *(unsigned long *)table->data) | |
2597 | scan_all_zones_unevictable_pages(); | |
2598 | ||
2599 | scan_unevictable_pages = 0; | |
2600 | return 0; | |
2601 | } | |
2602 | ||
2603 | /* | |
2604 | * per node 'scan_unevictable_pages' attribute. On demand re-scan of | |
2605 | * a specified node's per zone unevictable lists for evictable pages. | |
2606 | */ | |
2607 | ||
2608 | static ssize_t read_scan_unevictable_node(struct sys_device *dev, | |
2609 | struct sysdev_attribute *attr, | |
2610 | char *buf) | |
2611 | { | |
2612 | return sprintf(buf, "0\n"); /* always zero; should fit... */ | |
2613 | } | |
2614 | ||
2615 | static ssize_t write_scan_unevictable_node(struct sys_device *dev, | |
2616 | struct sysdev_attribute *attr, | |
2617 | const char *buf, size_t count) | |
2618 | { | |
2619 | struct zone *node_zones = NODE_DATA(dev->id)->node_zones; | |
2620 | struct zone *zone; | |
2621 | unsigned long res; | |
2622 | unsigned long req = strict_strtoul(buf, 10, &res); | |
2623 | ||
2624 | if (!req) | |
2625 | return 1; /* zero is no-op */ | |
2626 | ||
2627 | for (zone = node_zones; zone - node_zones < MAX_NR_ZONES; ++zone) { | |
2628 | if (!populated_zone(zone)) | |
2629 | continue; | |
2630 | scan_zone_unevictable_pages(zone); | |
2631 | } | |
2632 | return 1; | |
2633 | } | |
2634 | ||
2635 | ||
2636 | static SYSDEV_ATTR(scan_unevictable_pages, S_IRUGO | S_IWUSR, | |
2637 | read_scan_unevictable_node, | |
2638 | write_scan_unevictable_node); | |
2639 | ||
2640 | int scan_unevictable_register_node(struct node *node) | |
2641 | { | |
2642 | return sysdev_create_file(&node->sysdev, &attr_scan_unevictable_pages); | |
2643 | } | |
2644 | ||
2645 | void scan_unevictable_unregister_node(struct node *node) | |
2646 | { | |
2647 | sysdev_remove_file(&node->sysdev, &attr_scan_unevictable_pages); | |
2648 | } | |
2649 | ||
894bc310 | 2650 | #endif |