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> |
1da177e4 LT |
41 | |
42 | #include <asm/tlbflush.h> | |
43 | #include <asm/div64.h> | |
44 | ||
45 | #include <linux/swapops.h> | |
46 | ||
0f8053a5 NP |
47 | #include "internal.h" |
48 | ||
1da177e4 | 49 | struct scan_control { |
1da177e4 LT |
50 | /* Incremented by the number of inactive pages that were scanned */ |
51 | unsigned long nr_scanned; | |
52 | ||
1da177e4 | 53 | /* This context's GFP mask */ |
6daa0e28 | 54 | gfp_t gfp_mask; |
1da177e4 LT |
55 | |
56 | int may_writepage; | |
57 | ||
f1fd1067 CL |
58 | /* Can pages be swapped as part of reclaim? */ |
59 | int may_swap; | |
60 | ||
1da177e4 LT |
61 | /* This context's SWAP_CLUSTER_MAX. If freeing memory for |
62 | * suspend, we effectively ignore SWAP_CLUSTER_MAX. | |
63 | * In this context, it doesn't matter that we scan the | |
64 | * whole list at once. */ | |
65 | int swap_cluster_max; | |
d6277db4 RW |
66 | |
67 | int swappiness; | |
408d8544 NP |
68 | |
69 | int all_unreclaimable; | |
5ad333eb AW |
70 | |
71 | int order; | |
66e1707b | 72 | |
f1a9ee75 RR |
73 | /* |
74 | * Pages that have (or should have) IO pending. If we run into | |
75 | * a lot of these, we're better off waiting a little for IO to | |
76 | * finish rather than scanning more pages in the VM. | |
77 | */ | |
78 | int nr_io_pages; | |
79 | ||
66e1707b BS |
80 | /* Which cgroup do we reclaim from */ |
81 | struct mem_cgroup *mem_cgroup; | |
82 | ||
83 | /* Pluggable isolate pages callback */ | |
84 | unsigned long (*isolate_pages)(unsigned long nr, struct list_head *dst, | |
85 | unsigned long *scanned, int order, int mode, | |
86 | struct zone *z, struct mem_cgroup *mem_cont, | |
87 | int active); | |
1da177e4 LT |
88 | }; |
89 | ||
1da177e4 LT |
90 | #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) |
91 | ||
92 | #ifdef ARCH_HAS_PREFETCH | |
93 | #define prefetch_prev_lru_page(_page, _base, _field) \ | |
94 | do { \ | |
95 | if ((_page)->lru.prev != _base) { \ | |
96 | struct page *prev; \ | |
97 | \ | |
98 | prev = lru_to_page(&(_page->lru)); \ | |
99 | prefetch(&prev->_field); \ | |
100 | } \ | |
101 | } while (0) | |
102 | #else | |
103 | #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) | |
104 | #endif | |
105 | ||
106 | #ifdef ARCH_HAS_PREFETCHW | |
107 | #define prefetchw_prev_lru_page(_page, _base, _field) \ | |
108 | do { \ | |
109 | if ((_page)->lru.prev != _base) { \ | |
110 | struct page *prev; \ | |
111 | \ | |
112 | prev = lru_to_page(&(_page->lru)); \ | |
113 | prefetchw(&prev->_field); \ | |
114 | } \ | |
115 | } while (0) | |
116 | #else | |
117 | #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) | |
118 | #endif | |
119 | ||
120 | /* | |
121 | * From 0 .. 100. Higher means more swappy. | |
122 | */ | |
123 | int vm_swappiness = 60; | |
bd1e22b8 | 124 | long vm_total_pages; /* The total number of pages which the VM controls */ |
1da177e4 LT |
125 | |
126 | static LIST_HEAD(shrinker_list); | |
127 | static DECLARE_RWSEM(shrinker_rwsem); | |
128 | ||
129 | /* | |
130 | * Add a shrinker callback to be called from the vm | |
131 | */ | |
8e1f936b | 132 | void register_shrinker(struct shrinker *shrinker) |
1da177e4 | 133 | { |
8e1f936b RR |
134 | shrinker->nr = 0; |
135 | down_write(&shrinker_rwsem); | |
136 | list_add_tail(&shrinker->list, &shrinker_list); | |
137 | up_write(&shrinker_rwsem); | |
1da177e4 | 138 | } |
8e1f936b | 139 | EXPORT_SYMBOL(register_shrinker); |
1da177e4 LT |
140 | |
141 | /* | |
142 | * Remove one | |
143 | */ | |
8e1f936b | 144 | void unregister_shrinker(struct shrinker *shrinker) |
1da177e4 LT |
145 | { |
146 | down_write(&shrinker_rwsem); | |
147 | list_del(&shrinker->list); | |
148 | up_write(&shrinker_rwsem); | |
1da177e4 | 149 | } |
8e1f936b | 150 | EXPORT_SYMBOL(unregister_shrinker); |
1da177e4 LT |
151 | |
152 | #define SHRINK_BATCH 128 | |
153 | /* | |
154 | * Call the shrink functions to age shrinkable caches | |
155 | * | |
156 | * Here we assume it costs one seek to replace a lru page and that it also | |
157 | * takes a seek to recreate a cache object. With this in mind we age equal | |
158 | * percentages of the lru and ageable caches. This should balance the seeks | |
159 | * generated by these structures. | |
160 | * | |
183ff22b | 161 | * If the vm encountered mapped pages on the LRU it increase the pressure on |
1da177e4 LT |
162 | * slab to avoid swapping. |
163 | * | |
164 | * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. | |
165 | * | |
166 | * `lru_pages' represents the number of on-LRU pages in all the zones which | |
167 | * are eligible for the caller's allocation attempt. It is used for balancing | |
168 | * slab reclaim versus page reclaim. | |
b15e0905 | 169 | * |
170 | * Returns the number of slab objects which we shrunk. | |
1da177e4 | 171 | */ |
69e05944 AM |
172 | unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask, |
173 | unsigned long lru_pages) | |
1da177e4 LT |
174 | { |
175 | struct shrinker *shrinker; | |
69e05944 | 176 | unsigned long ret = 0; |
1da177e4 LT |
177 | |
178 | if (scanned == 0) | |
179 | scanned = SWAP_CLUSTER_MAX; | |
180 | ||
181 | if (!down_read_trylock(&shrinker_rwsem)) | |
b15e0905 | 182 | return 1; /* Assume we'll be able to shrink next time */ |
1da177e4 LT |
183 | |
184 | list_for_each_entry(shrinker, &shrinker_list, list) { | |
185 | unsigned long long delta; | |
186 | unsigned long total_scan; | |
8e1f936b | 187 | unsigned long max_pass = (*shrinker->shrink)(0, gfp_mask); |
1da177e4 LT |
188 | |
189 | delta = (4 * scanned) / shrinker->seeks; | |
ea164d73 | 190 | delta *= max_pass; |
1da177e4 LT |
191 | do_div(delta, lru_pages + 1); |
192 | shrinker->nr += delta; | |
ea164d73 AA |
193 | if (shrinker->nr < 0) { |
194 | printk(KERN_ERR "%s: nr=%ld\n", | |
195 | __FUNCTION__, shrinker->nr); | |
196 | shrinker->nr = max_pass; | |
197 | } | |
198 | ||
199 | /* | |
200 | * Avoid risking looping forever due to too large nr value: | |
201 | * never try to free more than twice the estimate number of | |
202 | * freeable entries. | |
203 | */ | |
204 | if (shrinker->nr > max_pass * 2) | |
205 | shrinker->nr = max_pass * 2; | |
1da177e4 LT |
206 | |
207 | total_scan = shrinker->nr; | |
208 | shrinker->nr = 0; | |
209 | ||
210 | while (total_scan >= SHRINK_BATCH) { | |
211 | long this_scan = SHRINK_BATCH; | |
212 | int shrink_ret; | |
b15e0905 | 213 | int nr_before; |
1da177e4 | 214 | |
8e1f936b RR |
215 | nr_before = (*shrinker->shrink)(0, gfp_mask); |
216 | shrink_ret = (*shrinker->shrink)(this_scan, gfp_mask); | |
1da177e4 LT |
217 | if (shrink_ret == -1) |
218 | break; | |
b15e0905 | 219 | if (shrink_ret < nr_before) |
220 | ret += nr_before - shrink_ret; | |
f8891e5e | 221 | count_vm_events(SLABS_SCANNED, this_scan); |
1da177e4 LT |
222 | total_scan -= this_scan; |
223 | ||
224 | cond_resched(); | |
225 | } | |
226 | ||
227 | shrinker->nr += total_scan; | |
228 | } | |
229 | up_read(&shrinker_rwsem); | |
b15e0905 | 230 | return ret; |
1da177e4 LT |
231 | } |
232 | ||
233 | /* Called without lock on whether page is mapped, so answer is unstable */ | |
234 | static inline int page_mapping_inuse(struct page *page) | |
235 | { | |
236 | struct address_space *mapping; | |
237 | ||
238 | /* Page is in somebody's page tables. */ | |
239 | if (page_mapped(page)) | |
240 | return 1; | |
241 | ||
242 | /* Be more reluctant to reclaim swapcache than pagecache */ | |
243 | if (PageSwapCache(page)) | |
244 | return 1; | |
245 | ||
246 | mapping = page_mapping(page); | |
247 | if (!mapping) | |
248 | return 0; | |
249 | ||
250 | /* File is mmap'd by somebody? */ | |
251 | return mapping_mapped(mapping); | |
252 | } | |
253 | ||
254 | static inline int is_page_cache_freeable(struct page *page) | |
255 | { | |
256 | return page_count(page) - !!PagePrivate(page) == 2; | |
257 | } | |
258 | ||
259 | static int may_write_to_queue(struct backing_dev_info *bdi) | |
260 | { | |
930d9152 | 261 | if (current->flags & PF_SWAPWRITE) |
1da177e4 LT |
262 | return 1; |
263 | if (!bdi_write_congested(bdi)) | |
264 | return 1; | |
265 | if (bdi == current->backing_dev_info) | |
266 | return 1; | |
267 | return 0; | |
268 | } | |
269 | ||
270 | /* | |
271 | * We detected a synchronous write error writing a page out. Probably | |
272 | * -ENOSPC. We need to propagate that into the address_space for a subsequent | |
273 | * fsync(), msync() or close(). | |
274 | * | |
275 | * The tricky part is that after writepage we cannot touch the mapping: nothing | |
276 | * prevents it from being freed up. But we have a ref on the page and once | |
277 | * that page is locked, the mapping is pinned. | |
278 | * | |
279 | * We're allowed to run sleeping lock_page() here because we know the caller has | |
280 | * __GFP_FS. | |
281 | */ | |
282 | static void handle_write_error(struct address_space *mapping, | |
283 | struct page *page, int error) | |
284 | { | |
285 | lock_page(page); | |
3e9f45bd GC |
286 | if (page_mapping(page) == mapping) |
287 | mapping_set_error(mapping, error); | |
1da177e4 LT |
288 | unlock_page(page); |
289 | } | |
290 | ||
c661b078 AW |
291 | /* Request for sync pageout. */ |
292 | enum pageout_io { | |
293 | PAGEOUT_IO_ASYNC, | |
294 | PAGEOUT_IO_SYNC, | |
295 | }; | |
296 | ||
04e62a29 CL |
297 | /* possible outcome of pageout() */ |
298 | typedef enum { | |
299 | /* failed to write page out, page is locked */ | |
300 | PAGE_KEEP, | |
301 | /* move page to the active list, page is locked */ | |
302 | PAGE_ACTIVATE, | |
303 | /* page has been sent to the disk successfully, page is unlocked */ | |
304 | PAGE_SUCCESS, | |
305 | /* page is clean and locked */ | |
306 | PAGE_CLEAN, | |
307 | } pageout_t; | |
308 | ||
1da177e4 | 309 | /* |
1742f19f AM |
310 | * pageout is called by shrink_page_list() for each dirty page. |
311 | * Calls ->writepage(). | |
1da177e4 | 312 | */ |
c661b078 AW |
313 | static pageout_t pageout(struct page *page, struct address_space *mapping, |
314 | enum pageout_io sync_writeback) | |
1da177e4 LT |
315 | { |
316 | /* | |
317 | * If the page is dirty, only perform writeback if that write | |
318 | * will be non-blocking. To prevent this allocation from being | |
319 | * stalled by pagecache activity. But note that there may be | |
320 | * stalls if we need to run get_block(). We could test | |
321 | * PagePrivate for that. | |
322 | * | |
323 | * If this process is currently in generic_file_write() against | |
324 | * this page's queue, we can perform writeback even if that | |
325 | * will block. | |
326 | * | |
327 | * If the page is swapcache, write it back even if that would | |
328 | * block, for some throttling. This happens by accident, because | |
329 | * swap_backing_dev_info is bust: it doesn't reflect the | |
330 | * congestion state of the swapdevs. Easy to fix, if needed. | |
331 | * See swapfile.c:page_queue_congested(). | |
332 | */ | |
333 | if (!is_page_cache_freeable(page)) | |
334 | return PAGE_KEEP; | |
335 | if (!mapping) { | |
336 | /* | |
337 | * Some data journaling orphaned pages can have | |
338 | * page->mapping == NULL while being dirty with clean buffers. | |
339 | */ | |
323aca6c | 340 | if (PagePrivate(page)) { |
1da177e4 LT |
341 | if (try_to_free_buffers(page)) { |
342 | ClearPageDirty(page); | |
343 | printk("%s: orphaned page\n", __FUNCTION__); | |
344 | return PAGE_CLEAN; | |
345 | } | |
346 | } | |
347 | return PAGE_KEEP; | |
348 | } | |
349 | if (mapping->a_ops->writepage == NULL) | |
350 | return PAGE_ACTIVATE; | |
351 | if (!may_write_to_queue(mapping->backing_dev_info)) | |
352 | return PAGE_KEEP; | |
353 | ||
354 | if (clear_page_dirty_for_io(page)) { | |
355 | int res; | |
356 | struct writeback_control wbc = { | |
357 | .sync_mode = WB_SYNC_NONE, | |
358 | .nr_to_write = SWAP_CLUSTER_MAX, | |
111ebb6e OH |
359 | .range_start = 0, |
360 | .range_end = LLONG_MAX, | |
1da177e4 LT |
361 | .nonblocking = 1, |
362 | .for_reclaim = 1, | |
363 | }; | |
364 | ||
365 | SetPageReclaim(page); | |
366 | res = mapping->a_ops->writepage(page, &wbc); | |
367 | if (res < 0) | |
368 | handle_write_error(mapping, page, res); | |
994fc28c | 369 | if (res == AOP_WRITEPAGE_ACTIVATE) { |
1da177e4 LT |
370 | ClearPageReclaim(page); |
371 | return PAGE_ACTIVATE; | |
372 | } | |
c661b078 AW |
373 | |
374 | /* | |
375 | * Wait on writeback if requested to. This happens when | |
376 | * direct reclaiming a large contiguous area and the | |
377 | * first attempt to free a range of pages fails. | |
378 | */ | |
379 | if (PageWriteback(page) && sync_writeback == PAGEOUT_IO_SYNC) | |
380 | wait_on_page_writeback(page); | |
381 | ||
1da177e4 LT |
382 | if (!PageWriteback(page)) { |
383 | /* synchronous write or broken a_ops? */ | |
384 | ClearPageReclaim(page); | |
385 | } | |
e129b5c2 | 386 | inc_zone_page_state(page, NR_VMSCAN_WRITE); |
1da177e4 LT |
387 | return PAGE_SUCCESS; |
388 | } | |
389 | ||
390 | return PAGE_CLEAN; | |
391 | } | |
392 | ||
a649fd92 AM |
393 | /* |
394 | * Attempt to detach a locked page from its ->mapping. If it is dirty or if | |
395 | * someone else has a ref on the page, abort and return 0. If it was | |
396 | * successfully detached, return 1. Assumes the caller has a single ref on | |
397 | * this page. | |
398 | */ | |
b20a3503 | 399 | int remove_mapping(struct address_space *mapping, struct page *page) |
49d2e9cc | 400 | { |
28e4d965 NP |
401 | BUG_ON(!PageLocked(page)); |
402 | BUG_ON(mapping != page_mapping(page)); | |
49d2e9cc CL |
403 | |
404 | write_lock_irq(&mapping->tree_lock); | |
49d2e9cc | 405 | /* |
0fd0e6b0 NP |
406 | * The non racy check for a busy page. |
407 | * | |
408 | * Must be careful with the order of the tests. When someone has | |
409 | * a ref to the page, it may be possible that they dirty it then | |
410 | * drop the reference. So if PageDirty is tested before page_count | |
411 | * here, then the following race may occur: | |
412 | * | |
413 | * get_user_pages(&page); | |
414 | * [user mapping goes away] | |
415 | * write_to(page); | |
416 | * !PageDirty(page) [good] | |
417 | * SetPageDirty(page); | |
418 | * put_page(page); | |
419 | * !page_count(page) [good, discard it] | |
420 | * | |
421 | * [oops, our write_to data is lost] | |
422 | * | |
423 | * Reversing the order of the tests ensures such a situation cannot | |
424 | * escape unnoticed. The smp_rmb is needed to ensure the page->flags | |
425 | * load is not satisfied before that of page->_count. | |
426 | * | |
427 | * Note that if SetPageDirty is always performed via set_page_dirty, | |
428 | * and thus under tree_lock, then this ordering is not required. | |
49d2e9cc CL |
429 | */ |
430 | if (unlikely(page_count(page) != 2)) | |
431 | goto cannot_free; | |
432 | smp_rmb(); | |
433 | if (unlikely(PageDirty(page))) | |
434 | goto cannot_free; | |
435 | ||
436 | if (PageSwapCache(page)) { | |
437 | swp_entry_t swap = { .val = page_private(page) }; | |
438 | __delete_from_swap_cache(page); | |
439 | write_unlock_irq(&mapping->tree_lock); | |
440 | swap_free(swap); | |
441 | __put_page(page); /* The pagecache ref */ | |
442 | return 1; | |
443 | } | |
444 | ||
445 | __remove_from_page_cache(page); | |
446 | write_unlock_irq(&mapping->tree_lock); | |
447 | __put_page(page); | |
448 | return 1; | |
449 | ||
450 | cannot_free: | |
451 | write_unlock_irq(&mapping->tree_lock); | |
452 | return 0; | |
453 | } | |
454 | ||
1da177e4 | 455 | /* |
1742f19f | 456 | * shrink_page_list() returns the number of reclaimed pages |
1da177e4 | 457 | */ |
1742f19f | 458 | static unsigned long shrink_page_list(struct list_head *page_list, |
c661b078 AW |
459 | struct scan_control *sc, |
460 | enum pageout_io sync_writeback) | |
1da177e4 LT |
461 | { |
462 | LIST_HEAD(ret_pages); | |
463 | struct pagevec freed_pvec; | |
464 | int pgactivate = 0; | |
05ff5137 | 465 | unsigned long nr_reclaimed = 0; |
1da177e4 LT |
466 | |
467 | cond_resched(); | |
468 | ||
469 | pagevec_init(&freed_pvec, 1); | |
470 | while (!list_empty(page_list)) { | |
471 | struct address_space *mapping; | |
472 | struct page *page; | |
473 | int may_enter_fs; | |
474 | int referenced; | |
475 | ||
476 | cond_resched(); | |
477 | ||
478 | page = lru_to_page(page_list); | |
479 | list_del(&page->lru); | |
480 | ||
481 | if (TestSetPageLocked(page)) | |
482 | goto keep; | |
483 | ||
725d704e | 484 | VM_BUG_ON(PageActive(page)); |
1da177e4 LT |
485 | |
486 | sc->nr_scanned++; | |
80e43426 CL |
487 | |
488 | if (!sc->may_swap && page_mapped(page)) | |
489 | goto keep_locked; | |
490 | ||
1da177e4 LT |
491 | /* Double the slab pressure for mapped and swapcache pages */ |
492 | if (page_mapped(page) || PageSwapCache(page)) | |
493 | sc->nr_scanned++; | |
494 | ||
c661b078 AW |
495 | may_enter_fs = (sc->gfp_mask & __GFP_FS) || |
496 | (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); | |
497 | ||
498 | if (PageWriteback(page)) { | |
499 | /* | |
500 | * Synchronous reclaim is performed in two passes, | |
501 | * first an asynchronous pass over the list to | |
502 | * start parallel writeback, and a second synchronous | |
503 | * pass to wait for the IO to complete. Wait here | |
504 | * for any page for which writeback has already | |
505 | * started. | |
506 | */ | |
507 | if (sync_writeback == PAGEOUT_IO_SYNC && may_enter_fs) | |
508 | wait_on_page_writeback(page); | |
f1a9ee75 RR |
509 | else { |
510 | sc->nr_io_pages++; | |
c661b078 | 511 | goto keep_locked; |
f1a9ee75 | 512 | } |
c661b078 | 513 | } |
1da177e4 | 514 | |
bed7161a | 515 | referenced = page_referenced(page, 1, sc->mem_cgroup); |
1da177e4 | 516 | /* In active use or really unfreeable? Activate it. */ |
5ad333eb AW |
517 | if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && |
518 | referenced && page_mapping_inuse(page)) | |
1da177e4 LT |
519 | goto activate_locked; |
520 | ||
521 | #ifdef CONFIG_SWAP | |
522 | /* | |
523 | * Anonymous process memory has backing store? | |
524 | * Try to allocate it some swap space here. | |
525 | */ | |
6e5ef1a9 | 526 | if (PageAnon(page) && !PageSwapCache(page)) |
1480a540 | 527 | if (!add_to_swap(page, GFP_ATOMIC)) |
1da177e4 | 528 | goto activate_locked; |
1da177e4 LT |
529 | #endif /* CONFIG_SWAP */ |
530 | ||
531 | mapping = page_mapping(page); | |
1da177e4 LT |
532 | |
533 | /* | |
534 | * The page is mapped into the page tables of one or more | |
535 | * processes. Try to unmap it here. | |
536 | */ | |
537 | if (page_mapped(page) && mapping) { | |
a48d07af | 538 | switch (try_to_unmap(page, 0)) { |
1da177e4 LT |
539 | case SWAP_FAIL: |
540 | goto activate_locked; | |
541 | case SWAP_AGAIN: | |
542 | goto keep_locked; | |
543 | case SWAP_SUCCESS: | |
544 | ; /* try to free the page below */ | |
545 | } | |
546 | } | |
547 | ||
548 | if (PageDirty(page)) { | |
5ad333eb | 549 | if (sc->order <= PAGE_ALLOC_COSTLY_ORDER && referenced) |
1da177e4 | 550 | goto keep_locked; |
f1a9ee75 RR |
551 | if (!may_enter_fs) { |
552 | sc->nr_io_pages++; | |
1da177e4 | 553 | goto keep_locked; |
f1a9ee75 | 554 | } |
52a8363e | 555 | if (!sc->may_writepage) |
1da177e4 LT |
556 | goto keep_locked; |
557 | ||
558 | /* Page is dirty, try to write it out here */ | |
c661b078 | 559 | switch (pageout(page, mapping, sync_writeback)) { |
1da177e4 LT |
560 | case PAGE_KEEP: |
561 | goto keep_locked; | |
562 | case PAGE_ACTIVATE: | |
563 | goto activate_locked; | |
564 | case PAGE_SUCCESS: | |
f1a9ee75 RR |
565 | if (PageWriteback(page) || PageDirty(page)) { |
566 | sc->nr_io_pages++; | |
1da177e4 | 567 | goto keep; |
f1a9ee75 | 568 | } |
1da177e4 LT |
569 | /* |
570 | * A synchronous write - probably a ramdisk. Go | |
571 | * ahead and try to reclaim the page. | |
572 | */ | |
573 | if (TestSetPageLocked(page)) | |
574 | goto keep; | |
575 | if (PageDirty(page) || PageWriteback(page)) | |
576 | goto keep_locked; | |
577 | mapping = page_mapping(page); | |
578 | case PAGE_CLEAN: | |
579 | ; /* try to free the page below */ | |
580 | } | |
581 | } | |
582 | ||
583 | /* | |
584 | * If the page has buffers, try to free the buffer mappings | |
585 | * associated with this page. If we succeed we try to free | |
586 | * the page as well. | |
587 | * | |
588 | * We do this even if the page is PageDirty(). | |
589 | * try_to_release_page() does not perform I/O, but it is | |
590 | * possible for a page to have PageDirty set, but it is actually | |
591 | * clean (all its buffers are clean). This happens if the | |
592 | * buffers were written out directly, with submit_bh(). ext3 | |
593 | * will do this, as well as the blockdev mapping. | |
594 | * try_to_release_page() will discover that cleanness and will | |
595 | * drop the buffers and mark the page clean - it can be freed. | |
596 | * | |
597 | * Rarely, pages can have buffers and no ->mapping. These are | |
598 | * the pages which were not successfully invalidated in | |
599 | * truncate_complete_page(). We try to drop those buffers here | |
600 | * and if that worked, and the page is no longer mapped into | |
601 | * process address space (page_count == 1) it can be freed. | |
602 | * Otherwise, leave the page on the LRU so it is swappable. | |
603 | */ | |
604 | if (PagePrivate(page)) { | |
605 | if (!try_to_release_page(page, sc->gfp_mask)) | |
606 | goto activate_locked; | |
607 | if (!mapping && page_count(page) == 1) | |
608 | goto free_it; | |
609 | } | |
610 | ||
28e4d965 | 611 | if (!mapping || !remove_mapping(mapping, page)) |
49d2e9cc | 612 | goto keep_locked; |
1da177e4 LT |
613 | |
614 | free_it: | |
615 | unlock_page(page); | |
05ff5137 | 616 | nr_reclaimed++; |
1da177e4 LT |
617 | if (!pagevec_add(&freed_pvec, page)) |
618 | __pagevec_release_nonlru(&freed_pvec); | |
619 | continue; | |
620 | ||
621 | activate_locked: | |
622 | SetPageActive(page); | |
623 | pgactivate++; | |
624 | keep_locked: | |
625 | unlock_page(page); | |
626 | keep: | |
627 | list_add(&page->lru, &ret_pages); | |
725d704e | 628 | VM_BUG_ON(PageLRU(page)); |
1da177e4 LT |
629 | } |
630 | list_splice(&ret_pages, page_list); | |
631 | if (pagevec_count(&freed_pvec)) | |
632 | __pagevec_release_nonlru(&freed_pvec); | |
f8891e5e | 633 | count_vm_events(PGACTIVATE, pgactivate); |
05ff5137 | 634 | return nr_reclaimed; |
1da177e4 LT |
635 | } |
636 | ||
5ad333eb AW |
637 | /* LRU Isolation modes. */ |
638 | #define ISOLATE_INACTIVE 0 /* Isolate inactive pages. */ | |
639 | #define ISOLATE_ACTIVE 1 /* Isolate active pages. */ | |
640 | #define ISOLATE_BOTH 2 /* Isolate both active and inactive pages. */ | |
641 | ||
642 | /* | |
643 | * Attempt to remove the specified page from its LRU. Only take this page | |
644 | * if it is of the appropriate PageActive status. Pages which are being | |
645 | * freed elsewhere are also ignored. | |
646 | * | |
647 | * page: page to consider | |
648 | * mode: one of the LRU isolation modes defined above | |
649 | * | |
650 | * returns 0 on success, -ve errno on failure. | |
651 | */ | |
66e1707b | 652 | int __isolate_lru_page(struct page *page, int mode) |
5ad333eb AW |
653 | { |
654 | int ret = -EINVAL; | |
655 | ||
656 | /* Only take pages on the LRU. */ | |
657 | if (!PageLRU(page)) | |
658 | return ret; | |
659 | ||
660 | /* | |
661 | * When checking the active state, we need to be sure we are | |
662 | * dealing with comparible boolean values. Take the logical not | |
663 | * of each. | |
664 | */ | |
665 | if (mode != ISOLATE_BOTH && (!PageActive(page) != !mode)) | |
666 | return ret; | |
667 | ||
668 | ret = -EBUSY; | |
669 | if (likely(get_page_unless_zero(page))) { | |
670 | /* | |
671 | * Be careful not to clear PageLRU until after we're | |
672 | * sure the page is not being freed elsewhere -- the | |
673 | * page release code relies on it. | |
674 | */ | |
675 | ClearPageLRU(page); | |
676 | ret = 0; | |
677 | } | |
678 | ||
679 | return ret; | |
680 | } | |
681 | ||
1da177e4 LT |
682 | /* |
683 | * zone->lru_lock is heavily contended. Some of the functions that | |
684 | * shrink the lists perform better by taking out a batch of pages | |
685 | * and working on them outside the LRU lock. | |
686 | * | |
687 | * For pagecache intensive workloads, this function is the hottest | |
688 | * spot in the kernel (apart from copy_*_user functions). | |
689 | * | |
690 | * Appropriate locks must be held before calling this function. | |
691 | * | |
692 | * @nr_to_scan: The number of pages to look through on the list. | |
693 | * @src: The LRU list to pull pages off. | |
694 | * @dst: The temp list to put pages on to. | |
695 | * @scanned: The number of pages that were scanned. | |
5ad333eb AW |
696 | * @order: The caller's attempted allocation order |
697 | * @mode: One of the LRU isolation modes | |
1da177e4 LT |
698 | * |
699 | * returns how many pages were moved onto *@dst. | |
700 | */ | |
69e05944 AM |
701 | static unsigned long isolate_lru_pages(unsigned long nr_to_scan, |
702 | struct list_head *src, struct list_head *dst, | |
5ad333eb | 703 | unsigned long *scanned, int order, int mode) |
1da177e4 | 704 | { |
69e05944 | 705 | unsigned long nr_taken = 0; |
c9b02d97 | 706 | unsigned long scan; |
1da177e4 | 707 | |
c9b02d97 | 708 | for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { |
5ad333eb AW |
709 | struct page *page; |
710 | unsigned long pfn; | |
711 | unsigned long end_pfn; | |
712 | unsigned long page_pfn; | |
713 | int zone_id; | |
714 | ||
1da177e4 LT |
715 | page = lru_to_page(src); |
716 | prefetchw_prev_lru_page(page, src, flags); | |
717 | ||
725d704e | 718 | VM_BUG_ON(!PageLRU(page)); |
8d438f96 | 719 | |
5ad333eb AW |
720 | switch (__isolate_lru_page(page, mode)) { |
721 | case 0: | |
722 | list_move(&page->lru, dst); | |
7c8ee9a8 | 723 | nr_taken++; |
5ad333eb AW |
724 | break; |
725 | ||
726 | case -EBUSY: | |
727 | /* else it is being freed elsewhere */ | |
728 | list_move(&page->lru, src); | |
729 | continue; | |
46453a6e | 730 | |
5ad333eb AW |
731 | default: |
732 | BUG(); | |
733 | } | |
734 | ||
735 | if (!order) | |
736 | continue; | |
737 | ||
738 | /* | |
739 | * Attempt to take all pages in the order aligned region | |
740 | * surrounding the tag page. Only take those pages of | |
741 | * the same active state as that tag page. We may safely | |
742 | * round the target page pfn down to the requested order | |
743 | * as the mem_map is guarenteed valid out to MAX_ORDER, | |
744 | * where that page is in a different zone we will detect | |
745 | * it from its zone id and abort this block scan. | |
746 | */ | |
747 | zone_id = page_zone_id(page); | |
748 | page_pfn = page_to_pfn(page); | |
749 | pfn = page_pfn & ~((1 << order) - 1); | |
750 | end_pfn = pfn + (1 << order); | |
751 | for (; pfn < end_pfn; pfn++) { | |
752 | struct page *cursor_page; | |
753 | ||
754 | /* The target page is in the block, ignore it. */ | |
755 | if (unlikely(pfn == page_pfn)) | |
756 | continue; | |
757 | ||
758 | /* Avoid holes within the zone. */ | |
759 | if (unlikely(!pfn_valid_within(pfn))) | |
760 | break; | |
761 | ||
762 | cursor_page = pfn_to_page(pfn); | |
763 | /* Check that we have not crossed a zone boundary. */ | |
764 | if (unlikely(page_zone_id(cursor_page) != zone_id)) | |
765 | continue; | |
766 | switch (__isolate_lru_page(cursor_page, mode)) { | |
767 | case 0: | |
768 | list_move(&cursor_page->lru, dst); | |
769 | nr_taken++; | |
770 | scan++; | |
771 | break; | |
772 | ||
773 | case -EBUSY: | |
774 | /* else it is being freed elsewhere */ | |
775 | list_move(&cursor_page->lru, src); | |
776 | default: | |
777 | break; | |
778 | } | |
779 | } | |
1da177e4 LT |
780 | } |
781 | ||
782 | *scanned = scan; | |
783 | return nr_taken; | |
784 | } | |
785 | ||
66e1707b BS |
786 | static unsigned long isolate_pages_global(unsigned long nr, |
787 | struct list_head *dst, | |
788 | unsigned long *scanned, int order, | |
789 | int mode, struct zone *z, | |
790 | struct mem_cgroup *mem_cont, | |
791 | int active) | |
792 | { | |
793 | if (active) | |
794 | return isolate_lru_pages(nr, &z->active_list, dst, | |
795 | scanned, order, mode); | |
796 | else | |
797 | return isolate_lru_pages(nr, &z->inactive_list, dst, | |
798 | scanned, order, mode); | |
799 | } | |
800 | ||
5ad333eb AW |
801 | /* |
802 | * clear_active_flags() is a helper for shrink_active_list(), clearing | |
803 | * any active bits from the pages in the list. | |
804 | */ | |
805 | static unsigned long clear_active_flags(struct list_head *page_list) | |
806 | { | |
807 | int nr_active = 0; | |
808 | struct page *page; | |
809 | ||
810 | list_for_each_entry(page, page_list, lru) | |
811 | if (PageActive(page)) { | |
812 | ClearPageActive(page); | |
813 | nr_active++; | |
814 | } | |
815 | ||
816 | return nr_active; | |
817 | } | |
818 | ||
1da177e4 | 819 | /* |
1742f19f AM |
820 | * shrink_inactive_list() is a helper for shrink_zone(). It returns the number |
821 | * of reclaimed pages | |
1da177e4 | 822 | */ |
1742f19f AM |
823 | static unsigned long shrink_inactive_list(unsigned long max_scan, |
824 | struct zone *zone, struct scan_control *sc) | |
1da177e4 LT |
825 | { |
826 | LIST_HEAD(page_list); | |
827 | struct pagevec pvec; | |
69e05944 | 828 | unsigned long nr_scanned = 0; |
05ff5137 | 829 | unsigned long nr_reclaimed = 0; |
1da177e4 LT |
830 | |
831 | pagevec_init(&pvec, 1); | |
832 | ||
833 | lru_add_drain(); | |
834 | spin_lock_irq(&zone->lru_lock); | |
69e05944 | 835 | do { |
1da177e4 | 836 | struct page *page; |
69e05944 AM |
837 | unsigned long nr_taken; |
838 | unsigned long nr_scan; | |
839 | unsigned long nr_freed; | |
5ad333eb | 840 | unsigned long nr_active; |
1da177e4 | 841 | |
66e1707b | 842 | nr_taken = sc->isolate_pages(sc->swap_cluster_max, |
5ad333eb AW |
843 | &page_list, &nr_scan, sc->order, |
844 | (sc->order > PAGE_ALLOC_COSTLY_ORDER)? | |
66e1707b BS |
845 | ISOLATE_BOTH : ISOLATE_INACTIVE, |
846 | zone, sc->mem_cgroup, 0); | |
5ad333eb | 847 | nr_active = clear_active_flags(&page_list); |
e9187bdc | 848 | __count_vm_events(PGDEACTIVATE, nr_active); |
5ad333eb AW |
849 | |
850 | __mod_zone_page_state(zone, NR_ACTIVE, -nr_active); | |
851 | __mod_zone_page_state(zone, NR_INACTIVE, | |
852 | -(nr_taken - nr_active)); | |
1da177e4 LT |
853 | zone->pages_scanned += nr_scan; |
854 | spin_unlock_irq(&zone->lru_lock); | |
855 | ||
69e05944 | 856 | nr_scanned += nr_scan; |
c661b078 AW |
857 | nr_freed = shrink_page_list(&page_list, sc, PAGEOUT_IO_ASYNC); |
858 | ||
859 | /* | |
860 | * If we are direct reclaiming for contiguous pages and we do | |
861 | * not reclaim everything in the list, try again and wait | |
862 | * for IO to complete. This will stall high-order allocations | |
863 | * but that should be acceptable to the caller | |
864 | */ | |
865 | if (nr_freed < nr_taken && !current_is_kswapd() && | |
866 | sc->order > PAGE_ALLOC_COSTLY_ORDER) { | |
867 | congestion_wait(WRITE, HZ/10); | |
868 | ||
869 | /* | |
870 | * The attempt at page out may have made some | |
871 | * of the pages active, mark them inactive again. | |
872 | */ | |
873 | nr_active = clear_active_flags(&page_list); | |
874 | count_vm_events(PGDEACTIVATE, nr_active); | |
875 | ||
876 | nr_freed += shrink_page_list(&page_list, sc, | |
877 | PAGEOUT_IO_SYNC); | |
878 | } | |
879 | ||
05ff5137 | 880 | nr_reclaimed += nr_freed; |
a74609fa NP |
881 | local_irq_disable(); |
882 | if (current_is_kswapd()) { | |
f8891e5e CL |
883 | __count_zone_vm_events(PGSCAN_KSWAPD, zone, nr_scan); |
884 | __count_vm_events(KSWAPD_STEAL, nr_freed); | |
a74609fa | 885 | } else |
f8891e5e | 886 | __count_zone_vm_events(PGSCAN_DIRECT, zone, nr_scan); |
918d3f90 | 887 | __count_zone_vm_events(PGSTEAL, zone, nr_freed); |
a74609fa | 888 | |
fb8d14e1 WF |
889 | if (nr_taken == 0) |
890 | goto done; | |
891 | ||
a74609fa | 892 | spin_lock(&zone->lru_lock); |
1da177e4 LT |
893 | /* |
894 | * Put back any unfreeable pages. | |
895 | */ | |
896 | while (!list_empty(&page_list)) { | |
897 | page = lru_to_page(&page_list); | |
725d704e | 898 | VM_BUG_ON(PageLRU(page)); |
8d438f96 | 899 | SetPageLRU(page); |
1da177e4 LT |
900 | list_del(&page->lru); |
901 | if (PageActive(page)) | |
902 | add_page_to_active_list(zone, page); | |
903 | else | |
904 | add_page_to_inactive_list(zone, page); | |
905 | if (!pagevec_add(&pvec, page)) { | |
906 | spin_unlock_irq(&zone->lru_lock); | |
907 | __pagevec_release(&pvec); | |
908 | spin_lock_irq(&zone->lru_lock); | |
909 | } | |
910 | } | |
69e05944 | 911 | } while (nr_scanned < max_scan); |
fb8d14e1 | 912 | spin_unlock(&zone->lru_lock); |
1da177e4 | 913 | done: |
fb8d14e1 | 914 | local_irq_enable(); |
1da177e4 | 915 | pagevec_release(&pvec); |
05ff5137 | 916 | return nr_reclaimed; |
1da177e4 LT |
917 | } |
918 | ||
3bb1a852 MB |
919 | /* |
920 | * We are about to scan this zone at a certain priority level. If that priority | |
921 | * level is smaller (ie: more urgent) than the previous priority, then note | |
922 | * that priority level within the zone. This is done so that when the next | |
923 | * process comes in to scan this zone, it will immediately start out at this | |
924 | * priority level rather than having to build up its own scanning priority. | |
925 | * Here, this priority affects only the reclaim-mapped threshold. | |
926 | */ | |
927 | static inline void note_zone_scanning_priority(struct zone *zone, int priority) | |
928 | { | |
929 | if (priority < zone->prev_priority) | |
930 | zone->prev_priority = priority; | |
931 | } | |
932 | ||
4ff1ffb4 NP |
933 | static inline int zone_is_near_oom(struct zone *zone) |
934 | { | |
c8785385 CL |
935 | return zone->pages_scanned >= (zone_page_state(zone, NR_ACTIVE) |
936 | + zone_page_state(zone, NR_INACTIVE))*3; | |
4ff1ffb4 NP |
937 | } |
938 | ||
1da177e4 LT |
939 | /* |
940 | * This moves pages from the active list to the inactive list. | |
941 | * | |
942 | * We move them the other way if the page is referenced by one or more | |
943 | * processes, from rmap. | |
944 | * | |
945 | * If the pages are mostly unmapped, the processing is fast and it is | |
946 | * appropriate to hold zone->lru_lock across the whole operation. But if | |
947 | * the pages are mapped, the processing is slow (page_referenced()) so we | |
948 | * should drop zone->lru_lock around each page. It's impossible to balance | |
949 | * this, so instead we remove the pages from the LRU while processing them. | |
950 | * It is safe to rely on PG_active against the non-LRU pages in here because | |
951 | * nobody will play with that bit on a non-LRU page. | |
952 | * | |
953 | * The downside is that we have to touch page->_count against each page. | |
954 | * But we had to alter page->flags anyway. | |
955 | */ | |
1742f19f | 956 | static void shrink_active_list(unsigned long nr_pages, struct zone *zone, |
bbdb396a | 957 | struct scan_control *sc, int priority) |
1da177e4 | 958 | { |
69e05944 | 959 | unsigned long pgmoved; |
1da177e4 | 960 | int pgdeactivate = 0; |
69e05944 | 961 | unsigned long pgscanned; |
1da177e4 LT |
962 | LIST_HEAD(l_hold); /* The pages which were snipped off */ |
963 | LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */ | |
964 | LIST_HEAD(l_active); /* Pages to go onto the active_list */ | |
965 | struct page *page; | |
966 | struct pagevec pvec; | |
967 | int reclaim_mapped = 0; | |
2903fb16 | 968 | |
6e5ef1a9 | 969 | if (sc->may_swap) { |
2903fb16 CL |
970 | long mapped_ratio; |
971 | long distress; | |
972 | long swap_tendency; | |
4106f83a | 973 | long imbalance; |
2903fb16 | 974 | |
4ff1ffb4 NP |
975 | if (zone_is_near_oom(zone)) |
976 | goto force_reclaim_mapped; | |
977 | ||
2903fb16 CL |
978 | /* |
979 | * `distress' is a measure of how much trouble we're having | |
980 | * reclaiming pages. 0 -> no problems. 100 -> great trouble. | |
981 | */ | |
bbdb396a | 982 | distress = 100 >> min(zone->prev_priority, priority); |
2903fb16 CL |
983 | |
984 | /* | |
985 | * The point of this algorithm is to decide when to start | |
986 | * reclaiming mapped memory instead of just pagecache. Work out | |
987 | * how much memory | |
988 | * is mapped. | |
989 | */ | |
f3dbd344 CL |
990 | mapped_ratio = ((global_page_state(NR_FILE_MAPPED) + |
991 | global_page_state(NR_ANON_PAGES)) * 100) / | |
bf02cf4b | 992 | vm_total_pages; |
2903fb16 CL |
993 | |
994 | /* | |
995 | * Now decide how much we really want to unmap some pages. The | |
996 | * mapped ratio is downgraded - just because there's a lot of | |
997 | * mapped memory doesn't necessarily mean that page reclaim | |
998 | * isn't succeeding. | |
999 | * | |
1000 | * The distress ratio is important - we don't want to start | |
1001 | * going oom. | |
1002 | * | |
1003 | * A 100% value of vm_swappiness overrides this algorithm | |
1004 | * altogether. | |
1005 | */ | |
d6277db4 | 1006 | swap_tendency = mapped_ratio / 2 + distress + sc->swappiness; |
2903fb16 | 1007 | |
4106f83a AA |
1008 | /* |
1009 | * If there's huge imbalance between active and inactive | |
1010 | * (think active 100 times larger than inactive) we should | |
1011 | * become more permissive, or the system will take too much | |
1012 | * cpu before it start swapping during memory pressure. | |
1013 | * Distress is about avoiding early-oom, this is about | |
1014 | * making swappiness graceful despite setting it to low | |
1015 | * values. | |
1016 | * | |
1017 | * Avoid div by zero with nr_inactive+1, and max resulting | |
1018 | * value is vm_total_pages. | |
1019 | */ | |
1020 | imbalance = zone_page_state(zone, NR_ACTIVE); | |
1021 | imbalance /= zone_page_state(zone, NR_INACTIVE) + 1; | |
1022 | ||
1023 | /* | |
1024 | * Reduce the effect of imbalance if swappiness is low, | |
1025 | * this means for a swappiness very low, the imbalance | |
1026 | * must be much higher than 100 for this logic to make | |
1027 | * the difference. | |
1028 | * | |
1029 | * Max temporary value is vm_total_pages*100. | |
1030 | */ | |
1031 | imbalance *= (vm_swappiness + 1); | |
1032 | imbalance /= 100; | |
1033 | ||
1034 | /* | |
1035 | * If not much of the ram is mapped, makes the imbalance | |
1036 | * less relevant, it's high priority we refill the inactive | |
1037 | * list with mapped pages only in presence of high ratio of | |
1038 | * mapped pages. | |
1039 | * | |
1040 | * Max temporary value is vm_total_pages*100. | |
1041 | */ | |
1042 | imbalance *= mapped_ratio; | |
1043 | imbalance /= 100; | |
1044 | ||
1045 | /* apply imbalance feedback to swap_tendency */ | |
1046 | swap_tendency += imbalance; | |
1047 | ||
2903fb16 CL |
1048 | /* |
1049 | * Now use this metric to decide whether to start moving mapped | |
1050 | * memory onto the inactive list. | |
1051 | */ | |
1052 | if (swap_tendency >= 100) | |
4ff1ffb4 | 1053 | force_reclaim_mapped: |
2903fb16 CL |
1054 | reclaim_mapped = 1; |
1055 | } | |
1da177e4 LT |
1056 | |
1057 | lru_add_drain(); | |
1058 | spin_lock_irq(&zone->lru_lock); | |
66e1707b BS |
1059 | pgmoved = sc->isolate_pages(nr_pages, &l_hold, &pgscanned, sc->order, |
1060 | ISOLATE_ACTIVE, zone, | |
1061 | sc->mem_cgroup, 1); | |
1da177e4 | 1062 | zone->pages_scanned += pgscanned; |
c8785385 | 1063 | __mod_zone_page_state(zone, NR_ACTIVE, -pgmoved); |
1da177e4 LT |
1064 | spin_unlock_irq(&zone->lru_lock); |
1065 | ||
1da177e4 LT |
1066 | while (!list_empty(&l_hold)) { |
1067 | cond_resched(); | |
1068 | page = lru_to_page(&l_hold); | |
1069 | list_del(&page->lru); | |
1070 | if (page_mapped(page)) { | |
1071 | if (!reclaim_mapped || | |
1072 | (total_swap_pages == 0 && PageAnon(page)) || | |
bed7161a | 1073 | page_referenced(page, 0, sc->mem_cgroup)) { |
1da177e4 LT |
1074 | list_add(&page->lru, &l_active); |
1075 | continue; | |
1076 | } | |
1077 | } | |
1078 | list_add(&page->lru, &l_inactive); | |
1079 | } | |
1080 | ||
1081 | pagevec_init(&pvec, 1); | |
1082 | pgmoved = 0; | |
1083 | spin_lock_irq(&zone->lru_lock); | |
1084 | while (!list_empty(&l_inactive)) { | |
1085 | page = lru_to_page(&l_inactive); | |
1086 | prefetchw_prev_lru_page(page, &l_inactive, flags); | |
725d704e | 1087 | VM_BUG_ON(PageLRU(page)); |
8d438f96 | 1088 | SetPageLRU(page); |
725d704e | 1089 | VM_BUG_ON(!PageActive(page)); |
4c84cacf NP |
1090 | ClearPageActive(page); |
1091 | ||
1da177e4 | 1092 | list_move(&page->lru, &zone->inactive_list); |
66e1707b | 1093 | mem_cgroup_move_lists(page_get_page_cgroup(page), false); |
1da177e4 LT |
1094 | pgmoved++; |
1095 | if (!pagevec_add(&pvec, page)) { | |
c8785385 | 1096 | __mod_zone_page_state(zone, NR_INACTIVE, pgmoved); |
1da177e4 LT |
1097 | spin_unlock_irq(&zone->lru_lock); |
1098 | pgdeactivate += pgmoved; | |
1099 | pgmoved = 0; | |
1100 | if (buffer_heads_over_limit) | |
1101 | pagevec_strip(&pvec); | |
1102 | __pagevec_release(&pvec); | |
1103 | spin_lock_irq(&zone->lru_lock); | |
1104 | } | |
1105 | } | |
c8785385 | 1106 | __mod_zone_page_state(zone, NR_INACTIVE, pgmoved); |
1da177e4 LT |
1107 | pgdeactivate += pgmoved; |
1108 | if (buffer_heads_over_limit) { | |
1109 | spin_unlock_irq(&zone->lru_lock); | |
1110 | pagevec_strip(&pvec); | |
1111 | spin_lock_irq(&zone->lru_lock); | |
1112 | } | |
1113 | ||
1114 | pgmoved = 0; | |
1115 | while (!list_empty(&l_active)) { | |
1116 | page = lru_to_page(&l_active); | |
1117 | prefetchw_prev_lru_page(page, &l_active, flags); | |
725d704e | 1118 | VM_BUG_ON(PageLRU(page)); |
8d438f96 | 1119 | SetPageLRU(page); |
725d704e | 1120 | VM_BUG_ON(!PageActive(page)); |
1da177e4 | 1121 | list_move(&page->lru, &zone->active_list); |
66e1707b | 1122 | mem_cgroup_move_lists(page_get_page_cgroup(page), true); |
1da177e4 LT |
1123 | pgmoved++; |
1124 | if (!pagevec_add(&pvec, page)) { | |
c8785385 | 1125 | __mod_zone_page_state(zone, NR_ACTIVE, pgmoved); |
1da177e4 LT |
1126 | pgmoved = 0; |
1127 | spin_unlock_irq(&zone->lru_lock); | |
1128 | __pagevec_release(&pvec); | |
1129 | spin_lock_irq(&zone->lru_lock); | |
1130 | } | |
1131 | } | |
c8785385 | 1132 | __mod_zone_page_state(zone, NR_ACTIVE, pgmoved); |
a74609fa | 1133 | |
f8891e5e CL |
1134 | __count_zone_vm_events(PGREFILL, zone, pgscanned); |
1135 | __count_vm_events(PGDEACTIVATE, pgdeactivate); | |
1136 | spin_unlock_irq(&zone->lru_lock); | |
1da177e4 | 1137 | |
a74609fa | 1138 | pagevec_release(&pvec); |
1da177e4 LT |
1139 | } |
1140 | ||
1141 | /* | |
1142 | * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. | |
1143 | */ | |
05ff5137 AM |
1144 | static unsigned long shrink_zone(int priority, struct zone *zone, |
1145 | struct scan_control *sc) | |
1da177e4 LT |
1146 | { |
1147 | unsigned long nr_active; | |
1148 | unsigned long nr_inactive; | |
8695949a | 1149 | unsigned long nr_to_scan; |
05ff5137 | 1150 | unsigned long nr_reclaimed = 0; |
1da177e4 LT |
1151 | |
1152 | /* | |
1153 | * Add one to `nr_to_scan' just to make sure that the kernel will | |
1154 | * slowly sift through the active list. | |
1155 | */ | |
c8785385 CL |
1156 | zone->nr_scan_active += |
1157 | (zone_page_state(zone, NR_ACTIVE) >> priority) + 1; | |
1da177e4 LT |
1158 | nr_active = zone->nr_scan_active; |
1159 | if (nr_active >= sc->swap_cluster_max) | |
1160 | zone->nr_scan_active = 0; | |
1161 | else | |
1162 | nr_active = 0; | |
1163 | ||
c8785385 CL |
1164 | zone->nr_scan_inactive += |
1165 | (zone_page_state(zone, NR_INACTIVE) >> priority) + 1; | |
1da177e4 LT |
1166 | nr_inactive = zone->nr_scan_inactive; |
1167 | if (nr_inactive >= sc->swap_cluster_max) | |
1168 | zone->nr_scan_inactive = 0; | |
1169 | else | |
1170 | nr_inactive = 0; | |
1171 | ||
1da177e4 LT |
1172 | while (nr_active || nr_inactive) { |
1173 | if (nr_active) { | |
8695949a | 1174 | nr_to_scan = min(nr_active, |
1da177e4 | 1175 | (unsigned long)sc->swap_cluster_max); |
8695949a | 1176 | nr_active -= nr_to_scan; |
bbdb396a | 1177 | shrink_active_list(nr_to_scan, zone, sc, priority); |
1da177e4 LT |
1178 | } |
1179 | ||
1180 | if (nr_inactive) { | |
8695949a | 1181 | nr_to_scan = min(nr_inactive, |
1da177e4 | 1182 | (unsigned long)sc->swap_cluster_max); |
8695949a | 1183 | nr_inactive -= nr_to_scan; |
1742f19f AM |
1184 | nr_reclaimed += shrink_inactive_list(nr_to_scan, zone, |
1185 | sc); | |
1da177e4 LT |
1186 | } |
1187 | } | |
1188 | ||
232ea4d6 | 1189 | throttle_vm_writeout(sc->gfp_mask); |
05ff5137 | 1190 | return nr_reclaimed; |
1da177e4 LT |
1191 | } |
1192 | ||
1193 | /* | |
1194 | * This is the direct reclaim path, for page-allocating processes. We only | |
1195 | * try to reclaim pages from zones which will satisfy the caller's allocation | |
1196 | * request. | |
1197 | * | |
1198 | * We reclaim from a zone even if that zone is over pages_high. Because: | |
1199 | * a) The caller may be trying to free *extra* pages to satisfy a higher-order | |
1200 | * allocation or | |
1201 | * b) The zones may be over pages_high but they must go *over* pages_high to | |
1202 | * satisfy the `incremental min' zone defense algorithm. | |
1203 | * | |
1204 | * Returns the number of reclaimed pages. | |
1205 | * | |
1206 | * If a zone is deemed to be full of pinned pages then just give it a light | |
1207 | * scan then give up on it. | |
1208 | */ | |
1742f19f | 1209 | static unsigned long shrink_zones(int priority, struct zone **zones, |
05ff5137 | 1210 | struct scan_control *sc) |
1da177e4 | 1211 | { |
05ff5137 | 1212 | unsigned long nr_reclaimed = 0; |
1da177e4 LT |
1213 | int i; |
1214 | ||
408d8544 | 1215 | sc->all_unreclaimable = 1; |
1da177e4 LT |
1216 | for (i = 0; zones[i] != NULL; i++) { |
1217 | struct zone *zone = zones[i]; | |
1218 | ||
f3fe6512 | 1219 | if (!populated_zone(zone)) |
1da177e4 LT |
1220 | continue; |
1221 | ||
02a0e53d | 1222 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1da177e4 LT |
1223 | continue; |
1224 | ||
3bb1a852 | 1225 | note_zone_scanning_priority(zone, priority); |
1da177e4 | 1226 | |
e815af95 | 1227 | if (zone_is_all_unreclaimable(zone) && priority != DEF_PRIORITY) |
1da177e4 LT |
1228 | continue; /* Let kswapd poll it */ |
1229 | ||
408d8544 NP |
1230 | sc->all_unreclaimable = 0; |
1231 | ||
05ff5137 | 1232 | nr_reclaimed += shrink_zone(priority, zone, sc); |
1da177e4 | 1233 | } |
05ff5137 | 1234 | return nr_reclaimed; |
1da177e4 LT |
1235 | } |
1236 | ||
1237 | /* | |
1238 | * This is the main entry point to direct page reclaim. | |
1239 | * | |
1240 | * If a full scan of the inactive list fails to free enough memory then we | |
1241 | * are "out of memory" and something needs to be killed. | |
1242 | * | |
1243 | * If the caller is !__GFP_FS then the probability of a failure is reasonably | |
1244 | * high - the zone may be full of dirty or under-writeback pages, which this | |
1245 | * caller can't do much about. We kick pdflush and take explicit naps in the | |
1246 | * hope that some of these pages can be written. But if the allocating task | |
1247 | * holds filesystem locks which prevent writeout this might not work, and the | |
1248 | * allocation attempt will fail. | |
1249 | */ | |
66e1707b BS |
1250 | static unsigned long do_try_to_free_pages(struct zone **zones, gfp_t gfp_mask, |
1251 | struct scan_control *sc) | |
1da177e4 LT |
1252 | { |
1253 | int priority; | |
1254 | int ret = 0; | |
69e05944 | 1255 | unsigned long total_scanned = 0; |
05ff5137 | 1256 | unsigned long nr_reclaimed = 0; |
1da177e4 | 1257 | struct reclaim_state *reclaim_state = current->reclaim_state; |
1da177e4 LT |
1258 | unsigned long lru_pages = 0; |
1259 | int i; | |
1da177e4 | 1260 | |
f8891e5e | 1261 | count_vm_event(ALLOCSTALL); |
1da177e4 LT |
1262 | |
1263 | for (i = 0; zones[i] != NULL; i++) { | |
1264 | struct zone *zone = zones[i]; | |
1265 | ||
02a0e53d | 1266 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1da177e4 LT |
1267 | continue; |
1268 | ||
c8785385 CL |
1269 | lru_pages += zone_page_state(zone, NR_ACTIVE) |
1270 | + zone_page_state(zone, NR_INACTIVE); | |
1da177e4 LT |
1271 | } |
1272 | ||
1273 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { | |
66e1707b | 1274 | sc->nr_scanned = 0; |
f1a9ee75 | 1275 | sc->nr_io_pages = 0; |
f7b7fd8f RR |
1276 | if (!priority) |
1277 | disable_swap_token(); | |
66e1707b BS |
1278 | nr_reclaimed += shrink_zones(priority, zones, sc); |
1279 | /* | |
1280 | * Don't shrink slabs when reclaiming memory from | |
1281 | * over limit cgroups | |
1282 | */ | |
1283 | if (sc->mem_cgroup == NULL) | |
1284 | shrink_slab(sc->nr_scanned, gfp_mask, lru_pages); | |
1da177e4 | 1285 | if (reclaim_state) { |
05ff5137 | 1286 | nr_reclaimed += reclaim_state->reclaimed_slab; |
1da177e4 LT |
1287 | reclaim_state->reclaimed_slab = 0; |
1288 | } | |
66e1707b BS |
1289 | total_scanned += sc->nr_scanned; |
1290 | if (nr_reclaimed >= sc->swap_cluster_max) { | |
1da177e4 LT |
1291 | ret = 1; |
1292 | goto out; | |
1293 | } | |
1294 | ||
1295 | /* | |
1296 | * Try to write back as many pages as we just scanned. This | |
1297 | * tends to cause slow streaming writers to write data to the | |
1298 | * disk smoothly, at the dirtying rate, which is nice. But | |
1299 | * that's undesirable in laptop mode, where we *want* lumpy | |
1300 | * writeout. So in laptop mode, write out the whole world. | |
1301 | */ | |
66e1707b BS |
1302 | if (total_scanned > sc->swap_cluster_max + |
1303 | sc->swap_cluster_max / 2) { | |
687a21ce | 1304 | wakeup_pdflush(laptop_mode ? 0 : total_scanned); |
66e1707b | 1305 | sc->may_writepage = 1; |
1da177e4 LT |
1306 | } |
1307 | ||
1308 | /* Take a nap, wait for some writeback to complete */ | |
f1a9ee75 RR |
1309 | if (sc->nr_scanned && priority < DEF_PRIORITY - 2 && |
1310 | sc->nr_io_pages > sc->swap_cluster_max) | |
3fcfab16 | 1311 | congestion_wait(WRITE, HZ/10); |
1da177e4 | 1312 | } |
408d8544 | 1313 | /* top priority shrink_caches still had more to do? don't OOM, then */ |
66e1707b | 1314 | if (!sc->all_unreclaimable && sc->mem_cgroup == NULL) |
408d8544 | 1315 | ret = 1; |
1da177e4 | 1316 | out: |
3bb1a852 MB |
1317 | /* |
1318 | * Now that we've scanned all the zones at this priority level, note | |
1319 | * that level within the zone so that the next thread which performs | |
1320 | * scanning of this zone will immediately start out at this priority | |
1321 | * level. This affects only the decision whether or not to bring | |
1322 | * mapped pages onto the inactive list. | |
1323 | */ | |
1324 | if (priority < 0) | |
1325 | priority = 0; | |
c80544dc | 1326 | for (i = 0; zones[i] != NULL; i++) { |
1da177e4 LT |
1327 | struct zone *zone = zones[i]; |
1328 | ||
02a0e53d | 1329 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1da177e4 LT |
1330 | continue; |
1331 | ||
3bb1a852 | 1332 | zone->prev_priority = priority; |
1da177e4 LT |
1333 | } |
1334 | return ret; | |
1335 | } | |
1336 | ||
66e1707b BS |
1337 | unsigned long try_to_free_pages(struct zone **zones, int order, gfp_t gfp_mask) |
1338 | { | |
1339 | struct scan_control sc = { | |
1340 | .gfp_mask = gfp_mask, | |
1341 | .may_writepage = !laptop_mode, | |
1342 | .swap_cluster_max = SWAP_CLUSTER_MAX, | |
1343 | .may_swap = 1, | |
1344 | .swappiness = vm_swappiness, | |
1345 | .order = order, | |
1346 | .mem_cgroup = NULL, | |
1347 | .isolate_pages = isolate_pages_global, | |
1348 | }; | |
1349 | ||
1350 | return do_try_to_free_pages(zones, gfp_mask, &sc); | |
1351 | } | |
1352 | ||
1353 | #ifdef CONFIG_CGROUP_MEM_CONT | |
1354 | ||
e1a1cd59 BS |
1355 | unsigned long try_to_free_mem_cgroup_pages(struct mem_cgroup *mem_cont, |
1356 | gfp_t gfp_mask) | |
66e1707b BS |
1357 | { |
1358 | struct scan_control sc = { | |
e1a1cd59 | 1359 | .gfp_mask = gfp_mask, |
66e1707b BS |
1360 | .may_writepage = !laptop_mode, |
1361 | .may_swap = 1, | |
1362 | .swap_cluster_max = SWAP_CLUSTER_MAX, | |
1363 | .swappiness = vm_swappiness, | |
1364 | .order = 0, | |
1365 | .mem_cgroup = mem_cont, | |
1366 | .isolate_pages = mem_cgroup_isolate_pages, | |
1367 | }; | |
66e1707b | 1368 | struct zone **zones; |
e1a1cd59 | 1369 | int target_zone = gfp_zone(GFP_HIGHUSER_MOVABLE); |
66e1707b | 1370 | |
417eead3 KH |
1371 | zones = NODE_DATA(numa_node_id())->node_zonelists[target_zone].zones; |
1372 | if (do_try_to_free_pages(zones, sc.gfp_mask, &sc)) | |
1373 | return 1; | |
66e1707b BS |
1374 | return 0; |
1375 | } | |
1376 | #endif | |
1377 | ||
1da177e4 LT |
1378 | /* |
1379 | * For kswapd, balance_pgdat() will work across all this node's zones until | |
1380 | * they are all at pages_high. | |
1381 | * | |
1da177e4 LT |
1382 | * Returns the number of pages which were actually freed. |
1383 | * | |
1384 | * There is special handling here for zones which are full of pinned pages. | |
1385 | * This can happen if the pages are all mlocked, or if they are all used by | |
1386 | * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. | |
1387 | * What we do is to detect the case where all pages in the zone have been | |
1388 | * scanned twice and there has been zero successful reclaim. Mark the zone as | |
1389 | * dead and from now on, only perform a short scan. Basically we're polling | |
1390 | * the zone for when the problem goes away. | |
1391 | * | |
1392 | * kswapd scans the zones in the highmem->normal->dma direction. It skips | |
1393 | * zones which have free_pages > pages_high, but once a zone is found to have | |
1394 | * free_pages <= pages_high, we scan that zone and the lower zones regardless | |
1395 | * of the number of free pages in the lower zones. This interoperates with | |
1396 | * the page allocator fallback scheme to ensure that aging of pages is balanced | |
1397 | * across the zones. | |
1398 | */ | |
d6277db4 | 1399 | static unsigned long balance_pgdat(pg_data_t *pgdat, int order) |
1da177e4 | 1400 | { |
1da177e4 LT |
1401 | int all_zones_ok; |
1402 | int priority; | |
1403 | int i; | |
69e05944 | 1404 | unsigned long total_scanned; |
05ff5137 | 1405 | unsigned long nr_reclaimed; |
1da177e4 | 1406 | struct reclaim_state *reclaim_state = current->reclaim_state; |
179e9639 AM |
1407 | struct scan_control sc = { |
1408 | .gfp_mask = GFP_KERNEL, | |
1409 | .may_swap = 1, | |
d6277db4 RW |
1410 | .swap_cluster_max = SWAP_CLUSTER_MAX, |
1411 | .swappiness = vm_swappiness, | |
5ad333eb | 1412 | .order = order, |
66e1707b BS |
1413 | .mem_cgroup = NULL, |
1414 | .isolate_pages = isolate_pages_global, | |
179e9639 | 1415 | }; |
3bb1a852 MB |
1416 | /* |
1417 | * temp_priority is used to remember the scanning priority at which | |
1418 | * this zone was successfully refilled to free_pages == pages_high. | |
1419 | */ | |
1420 | int temp_priority[MAX_NR_ZONES]; | |
1da177e4 LT |
1421 | |
1422 | loop_again: | |
1423 | total_scanned = 0; | |
05ff5137 | 1424 | nr_reclaimed = 0; |
c0bbbc73 | 1425 | sc.may_writepage = !laptop_mode; |
f8891e5e | 1426 | count_vm_event(PAGEOUTRUN); |
1da177e4 | 1427 | |
3bb1a852 MB |
1428 | for (i = 0; i < pgdat->nr_zones; i++) |
1429 | temp_priority[i] = DEF_PRIORITY; | |
1da177e4 LT |
1430 | |
1431 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { | |
1432 | int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ | |
1433 | unsigned long lru_pages = 0; | |
1434 | ||
f7b7fd8f RR |
1435 | /* The swap token gets in the way of swapout... */ |
1436 | if (!priority) | |
1437 | disable_swap_token(); | |
1438 | ||
f1a9ee75 | 1439 | sc.nr_io_pages = 0; |
1da177e4 LT |
1440 | all_zones_ok = 1; |
1441 | ||
d6277db4 RW |
1442 | /* |
1443 | * Scan in the highmem->dma direction for the highest | |
1444 | * zone which needs scanning | |
1445 | */ | |
1446 | for (i = pgdat->nr_zones - 1; i >= 0; i--) { | |
1447 | struct zone *zone = pgdat->node_zones + i; | |
1da177e4 | 1448 | |
d6277db4 RW |
1449 | if (!populated_zone(zone)) |
1450 | continue; | |
1da177e4 | 1451 | |
e815af95 DR |
1452 | if (zone_is_all_unreclaimable(zone) && |
1453 | priority != DEF_PRIORITY) | |
d6277db4 | 1454 | continue; |
1da177e4 | 1455 | |
d6277db4 RW |
1456 | if (!zone_watermark_ok(zone, order, zone->pages_high, |
1457 | 0, 0)) { | |
1458 | end_zone = i; | |
e1dbeda6 | 1459 | break; |
1da177e4 | 1460 | } |
1da177e4 | 1461 | } |
e1dbeda6 AM |
1462 | if (i < 0) |
1463 | goto out; | |
1464 | ||
1da177e4 LT |
1465 | for (i = 0; i <= end_zone; i++) { |
1466 | struct zone *zone = pgdat->node_zones + i; | |
1467 | ||
c8785385 CL |
1468 | lru_pages += zone_page_state(zone, NR_ACTIVE) |
1469 | + zone_page_state(zone, NR_INACTIVE); | |
1da177e4 LT |
1470 | } |
1471 | ||
1472 | /* | |
1473 | * Now scan the zone in the dma->highmem direction, stopping | |
1474 | * at the last zone which needs scanning. | |
1475 | * | |
1476 | * We do this because the page allocator works in the opposite | |
1477 | * direction. This prevents the page allocator from allocating | |
1478 | * pages behind kswapd's direction of progress, which would | |
1479 | * cause too much scanning of the lower zones. | |
1480 | */ | |
1481 | for (i = 0; i <= end_zone; i++) { | |
1482 | struct zone *zone = pgdat->node_zones + i; | |
b15e0905 | 1483 | int nr_slab; |
1da177e4 | 1484 | |
f3fe6512 | 1485 | if (!populated_zone(zone)) |
1da177e4 LT |
1486 | continue; |
1487 | ||
e815af95 DR |
1488 | if (zone_is_all_unreclaimable(zone) && |
1489 | priority != DEF_PRIORITY) | |
1da177e4 LT |
1490 | continue; |
1491 | ||
d6277db4 RW |
1492 | if (!zone_watermark_ok(zone, order, zone->pages_high, |
1493 | end_zone, 0)) | |
1494 | all_zones_ok = 0; | |
3bb1a852 | 1495 | temp_priority[i] = priority; |
1da177e4 | 1496 | sc.nr_scanned = 0; |
3bb1a852 | 1497 | note_zone_scanning_priority(zone, priority); |
32a4330d RR |
1498 | /* |
1499 | * We put equal pressure on every zone, unless one | |
1500 | * zone has way too many pages free already. | |
1501 | */ | |
1502 | if (!zone_watermark_ok(zone, order, 8*zone->pages_high, | |
1503 | end_zone, 0)) | |
1504 | nr_reclaimed += shrink_zone(priority, zone, &sc); | |
1da177e4 | 1505 | reclaim_state->reclaimed_slab = 0; |
b15e0905 | 1506 | nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL, |
1507 | lru_pages); | |
05ff5137 | 1508 | nr_reclaimed += reclaim_state->reclaimed_slab; |
1da177e4 | 1509 | total_scanned += sc.nr_scanned; |
e815af95 | 1510 | if (zone_is_all_unreclaimable(zone)) |
1da177e4 | 1511 | continue; |
b15e0905 | 1512 | if (nr_slab == 0 && zone->pages_scanned >= |
c8785385 CL |
1513 | (zone_page_state(zone, NR_ACTIVE) |
1514 | + zone_page_state(zone, NR_INACTIVE)) * 6) | |
e815af95 DR |
1515 | zone_set_flag(zone, |
1516 | ZONE_ALL_UNRECLAIMABLE); | |
1da177e4 LT |
1517 | /* |
1518 | * If we've done a decent amount of scanning and | |
1519 | * the reclaim ratio is low, start doing writepage | |
1520 | * even in laptop mode | |
1521 | */ | |
1522 | if (total_scanned > SWAP_CLUSTER_MAX * 2 && | |
05ff5137 | 1523 | total_scanned > nr_reclaimed + nr_reclaimed / 2) |
1da177e4 LT |
1524 | sc.may_writepage = 1; |
1525 | } | |
1da177e4 LT |
1526 | if (all_zones_ok) |
1527 | break; /* kswapd: all done */ | |
1528 | /* | |
1529 | * OK, kswapd is getting into trouble. Take a nap, then take | |
1530 | * another pass across the zones. | |
1531 | */ | |
f1a9ee75 RR |
1532 | if (total_scanned && priority < DEF_PRIORITY - 2 && |
1533 | sc.nr_io_pages > sc.swap_cluster_max) | |
3fcfab16 | 1534 | congestion_wait(WRITE, HZ/10); |
1da177e4 LT |
1535 | |
1536 | /* | |
1537 | * We do this so kswapd doesn't build up large priorities for | |
1538 | * example when it is freeing in parallel with allocators. It | |
1539 | * matches the direct reclaim path behaviour in terms of impact | |
1540 | * on zone->*_priority. | |
1541 | */ | |
d6277db4 | 1542 | if (nr_reclaimed >= SWAP_CLUSTER_MAX) |
1da177e4 LT |
1543 | break; |
1544 | } | |
1545 | out: | |
3bb1a852 MB |
1546 | /* |
1547 | * Note within each zone the priority level at which this zone was | |
1548 | * brought into a happy state. So that the next thread which scans this | |
1549 | * zone will start out at that priority level. | |
1550 | */ | |
1da177e4 LT |
1551 | for (i = 0; i < pgdat->nr_zones; i++) { |
1552 | struct zone *zone = pgdat->node_zones + i; | |
1553 | ||
3bb1a852 | 1554 | zone->prev_priority = temp_priority[i]; |
1da177e4 LT |
1555 | } |
1556 | if (!all_zones_ok) { | |
1557 | cond_resched(); | |
8357376d RW |
1558 | |
1559 | try_to_freeze(); | |
1560 | ||
1da177e4 LT |
1561 | goto loop_again; |
1562 | } | |
1563 | ||
05ff5137 | 1564 | return nr_reclaimed; |
1da177e4 LT |
1565 | } |
1566 | ||
1567 | /* | |
1568 | * The background pageout daemon, started as a kernel thread | |
1569 | * from the init process. | |
1570 | * | |
1571 | * This basically trickles out pages so that we have _some_ | |
1572 | * free memory available even if there is no other activity | |
1573 | * that frees anything up. This is needed for things like routing | |
1574 | * etc, where we otherwise might have all activity going on in | |
1575 | * asynchronous contexts that cannot page things out. | |
1576 | * | |
1577 | * If there are applications that are active memory-allocators | |
1578 | * (most normal use), this basically shouldn't matter. | |
1579 | */ | |
1580 | static int kswapd(void *p) | |
1581 | { | |
1582 | unsigned long order; | |
1583 | pg_data_t *pgdat = (pg_data_t*)p; | |
1584 | struct task_struct *tsk = current; | |
1585 | DEFINE_WAIT(wait); | |
1586 | struct reclaim_state reclaim_state = { | |
1587 | .reclaimed_slab = 0, | |
1588 | }; | |
1589 | cpumask_t cpumask; | |
1590 | ||
1da177e4 LT |
1591 | cpumask = node_to_cpumask(pgdat->node_id); |
1592 | if (!cpus_empty(cpumask)) | |
1593 | set_cpus_allowed(tsk, cpumask); | |
1594 | current->reclaim_state = &reclaim_state; | |
1595 | ||
1596 | /* | |
1597 | * Tell the memory management that we're a "memory allocator", | |
1598 | * and that if we need more memory we should get access to it | |
1599 | * regardless (see "__alloc_pages()"). "kswapd" should | |
1600 | * never get caught in the normal page freeing logic. | |
1601 | * | |
1602 | * (Kswapd normally doesn't need memory anyway, but sometimes | |
1603 | * you need a small amount of memory in order to be able to | |
1604 | * page out something else, and this flag essentially protects | |
1605 | * us from recursively trying to free more memory as we're | |
1606 | * trying to free the first piece of memory in the first place). | |
1607 | */ | |
930d9152 | 1608 | tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; |
83144186 | 1609 | set_freezable(); |
1da177e4 LT |
1610 | |
1611 | order = 0; | |
1612 | for ( ; ; ) { | |
1613 | unsigned long new_order; | |
3e1d1d28 | 1614 | |
1da177e4 LT |
1615 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); |
1616 | new_order = pgdat->kswapd_max_order; | |
1617 | pgdat->kswapd_max_order = 0; | |
1618 | if (order < new_order) { | |
1619 | /* | |
1620 | * Don't sleep if someone wants a larger 'order' | |
1621 | * allocation | |
1622 | */ | |
1623 | order = new_order; | |
1624 | } else { | |
b1296cc4 RW |
1625 | if (!freezing(current)) |
1626 | schedule(); | |
1627 | ||
1da177e4 LT |
1628 | order = pgdat->kswapd_max_order; |
1629 | } | |
1630 | finish_wait(&pgdat->kswapd_wait, &wait); | |
1631 | ||
b1296cc4 RW |
1632 | if (!try_to_freeze()) { |
1633 | /* We can speed up thawing tasks if we don't call | |
1634 | * balance_pgdat after returning from the refrigerator | |
1635 | */ | |
1636 | balance_pgdat(pgdat, order); | |
1637 | } | |
1da177e4 LT |
1638 | } |
1639 | return 0; | |
1640 | } | |
1641 | ||
1642 | /* | |
1643 | * A zone is low on free memory, so wake its kswapd task to service it. | |
1644 | */ | |
1645 | void wakeup_kswapd(struct zone *zone, int order) | |
1646 | { | |
1647 | pg_data_t *pgdat; | |
1648 | ||
f3fe6512 | 1649 | if (!populated_zone(zone)) |
1da177e4 LT |
1650 | return; |
1651 | ||
1652 | pgdat = zone->zone_pgdat; | |
7fb1d9fc | 1653 | if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0)) |
1da177e4 LT |
1654 | return; |
1655 | if (pgdat->kswapd_max_order < order) | |
1656 | pgdat->kswapd_max_order = order; | |
02a0e53d | 1657 | if (!cpuset_zone_allowed_hardwall(zone, GFP_KERNEL)) |
1da177e4 | 1658 | return; |
8d0986e2 | 1659 | if (!waitqueue_active(&pgdat->kswapd_wait)) |
1da177e4 | 1660 | return; |
8d0986e2 | 1661 | wake_up_interruptible(&pgdat->kswapd_wait); |
1da177e4 LT |
1662 | } |
1663 | ||
1664 | #ifdef CONFIG_PM | |
1665 | /* | |
d6277db4 RW |
1666 | * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages |
1667 | * from LRU lists system-wide, for given pass and priority, and returns the | |
1668 | * number of reclaimed pages | |
1669 | * | |
1670 | * For pass > 3 we also try to shrink the LRU lists that contain a few pages | |
1671 | */ | |
e07aa05b NC |
1672 | static unsigned long shrink_all_zones(unsigned long nr_pages, int prio, |
1673 | int pass, struct scan_control *sc) | |
d6277db4 RW |
1674 | { |
1675 | struct zone *zone; | |
1676 | unsigned long nr_to_scan, ret = 0; | |
1677 | ||
1678 | for_each_zone(zone) { | |
1679 | ||
1680 | if (!populated_zone(zone)) | |
1681 | continue; | |
1682 | ||
e815af95 | 1683 | if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY) |
d6277db4 RW |
1684 | continue; |
1685 | ||
1686 | /* For pass = 0 we don't shrink the active list */ | |
1687 | if (pass > 0) { | |
c8785385 CL |
1688 | zone->nr_scan_active += |
1689 | (zone_page_state(zone, NR_ACTIVE) >> prio) + 1; | |
d6277db4 RW |
1690 | if (zone->nr_scan_active >= nr_pages || pass > 3) { |
1691 | zone->nr_scan_active = 0; | |
c8785385 CL |
1692 | nr_to_scan = min(nr_pages, |
1693 | zone_page_state(zone, NR_ACTIVE)); | |
bbdb396a | 1694 | shrink_active_list(nr_to_scan, zone, sc, prio); |
d6277db4 RW |
1695 | } |
1696 | } | |
1697 | ||
c8785385 CL |
1698 | zone->nr_scan_inactive += |
1699 | (zone_page_state(zone, NR_INACTIVE) >> prio) + 1; | |
d6277db4 RW |
1700 | if (zone->nr_scan_inactive >= nr_pages || pass > 3) { |
1701 | zone->nr_scan_inactive = 0; | |
c8785385 CL |
1702 | nr_to_scan = min(nr_pages, |
1703 | zone_page_state(zone, NR_INACTIVE)); | |
d6277db4 RW |
1704 | ret += shrink_inactive_list(nr_to_scan, zone, sc); |
1705 | if (ret >= nr_pages) | |
1706 | return ret; | |
1707 | } | |
1708 | } | |
1709 | ||
1710 | return ret; | |
1711 | } | |
1712 | ||
76395d37 AM |
1713 | static unsigned long count_lru_pages(void) |
1714 | { | |
c8785385 | 1715 | return global_page_state(NR_ACTIVE) + global_page_state(NR_INACTIVE); |
76395d37 AM |
1716 | } |
1717 | ||
d6277db4 RW |
1718 | /* |
1719 | * Try to free `nr_pages' of memory, system-wide, and return the number of | |
1720 | * freed pages. | |
1721 | * | |
1722 | * Rather than trying to age LRUs the aim is to preserve the overall | |
1723 | * LRU order by reclaiming preferentially | |
1724 | * inactive > active > active referenced > active mapped | |
1da177e4 | 1725 | */ |
69e05944 | 1726 | unsigned long shrink_all_memory(unsigned long nr_pages) |
1da177e4 | 1727 | { |
d6277db4 | 1728 | unsigned long lru_pages, nr_slab; |
69e05944 | 1729 | unsigned long ret = 0; |
d6277db4 RW |
1730 | int pass; |
1731 | struct reclaim_state reclaim_state; | |
d6277db4 RW |
1732 | struct scan_control sc = { |
1733 | .gfp_mask = GFP_KERNEL, | |
1734 | .may_swap = 0, | |
1735 | .swap_cluster_max = nr_pages, | |
1736 | .may_writepage = 1, | |
1737 | .swappiness = vm_swappiness, | |
66e1707b | 1738 | .isolate_pages = isolate_pages_global, |
1da177e4 LT |
1739 | }; |
1740 | ||
1741 | current->reclaim_state = &reclaim_state; | |
69e05944 | 1742 | |
76395d37 | 1743 | lru_pages = count_lru_pages(); |
972d1a7b | 1744 | nr_slab = global_page_state(NR_SLAB_RECLAIMABLE); |
d6277db4 RW |
1745 | /* If slab caches are huge, it's better to hit them first */ |
1746 | while (nr_slab >= lru_pages) { | |
1747 | reclaim_state.reclaimed_slab = 0; | |
1748 | shrink_slab(nr_pages, sc.gfp_mask, lru_pages); | |
1749 | if (!reclaim_state.reclaimed_slab) | |
1da177e4 | 1750 | break; |
d6277db4 RW |
1751 | |
1752 | ret += reclaim_state.reclaimed_slab; | |
1753 | if (ret >= nr_pages) | |
1754 | goto out; | |
1755 | ||
1756 | nr_slab -= reclaim_state.reclaimed_slab; | |
1da177e4 | 1757 | } |
d6277db4 RW |
1758 | |
1759 | /* | |
1760 | * We try to shrink LRUs in 5 passes: | |
1761 | * 0 = Reclaim from inactive_list only | |
1762 | * 1 = Reclaim from active list but don't reclaim mapped | |
1763 | * 2 = 2nd pass of type 1 | |
1764 | * 3 = Reclaim mapped (normal reclaim) | |
1765 | * 4 = 2nd pass of type 3 | |
1766 | */ | |
1767 | for (pass = 0; pass < 5; pass++) { | |
1768 | int prio; | |
1769 | ||
d6277db4 RW |
1770 | /* Force reclaiming mapped pages in the passes #3 and #4 */ |
1771 | if (pass > 2) { | |
1772 | sc.may_swap = 1; | |
1773 | sc.swappiness = 100; | |
1774 | } | |
1775 | ||
1776 | for (prio = DEF_PRIORITY; prio >= 0; prio--) { | |
1777 | unsigned long nr_to_scan = nr_pages - ret; | |
1778 | ||
d6277db4 | 1779 | sc.nr_scanned = 0; |
d6277db4 RW |
1780 | ret += shrink_all_zones(nr_to_scan, prio, pass, &sc); |
1781 | if (ret >= nr_pages) | |
1782 | goto out; | |
1783 | ||
1784 | reclaim_state.reclaimed_slab = 0; | |
76395d37 AM |
1785 | shrink_slab(sc.nr_scanned, sc.gfp_mask, |
1786 | count_lru_pages()); | |
d6277db4 RW |
1787 | ret += reclaim_state.reclaimed_slab; |
1788 | if (ret >= nr_pages) | |
1789 | goto out; | |
1790 | ||
1791 | if (sc.nr_scanned && prio < DEF_PRIORITY - 2) | |
3fcfab16 | 1792 | congestion_wait(WRITE, HZ / 10); |
d6277db4 | 1793 | } |
248a0301 | 1794 | } |
d6277db4 RW |
1795 | |
1796 | /* | |
1797 | * If ret = 0, we could not shrink LRUs, but there may be something | |
1798 | * in slab caches | |
1799 | */ | |
76395d37 | 1800 | if (!ret) { |
d6277db4 RW |
1801 | do { |
1802 | reclaim_state.reclaimed_slab = 0; | |
76395d37 | 1803 | shrink_slab(nr_pages, sc.gfp_mask, count_lru_pages()); |
d6277db4 RW |
1804 | ret += reclaim_state.reclaimed_slab; |
1805 | } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0); | |
76395d37 | 1806 | } |
d6277db4 RW |
1807 | |
1808 | out: | |
1da177e4 | 1809 | current->reclaim_state = NULL; |
d6277db4 | 1810 | |
1da177e4 LT |
1811 | return ret; |
1812 | } | |
1813 | #endif | |
1814 | ||
1da177e4 LT |
1815 | /* It's optimal to keep kswapds on the same CPUs as their memory, but |
1816 | not required for correctness. So if the last cpu in a node goes | |
1817 | away, we get changed to run anywhere: as the first one comes back, | |
1818 | restore their cpu bindings. */ | |
9c7b216d | 1819 | static int __devinit cpu_callback(struct notifier_block *nfb, |
69e05944 | 1820 | unsigned long action, void *hcpu) |
1da177e4 LT |
1821 | { |
1822 | pg_data_t *pgdat; | |
1823 | cpumask_t mask; | |
58c0a4a7 | 1824 | int nid; |
1da177e4 | 1825 | |
8bb78442 | 1826 | if (action == CPU_ONLINE || action == CPU_ONLINE_FROZEN) { |
58c0a4a7 YG |
1827 | for_each_node_state(nid, N_HIGH_MEMORY) { |
1828 | pgdat = NODE_DATA(nid); | |
1da177e4 LT |
1829 | mask = node_to_cpumask(pgdat->node_id); |
1830 | if (any_online_cpu(mask) != NR_CPUS) | |
1831 | /* One of our CPUs online: restore mask */ | |
1832 | set_cpus_allowed(pgdat->kswapd, mask); | |
1833 | } | |
1834 | } | |
1835 | return NOTIFY_OK; | |
1836 | } | |
1da177e4 | 1837 | |
3218ae14 YG |
1838 | /* |
1839 | * This kswapd start function will be called by init and node-hot-add. | |
1840 | * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. | |
1841 | */ | |
1842 | int kswapd_run(int nid) | |
1843 | { | |
1844 | pg_data_t *pgdat = NODE_DATA(nid); | |
1845 | int ret = 0; | |
1846 | ||
1847 | if (pgdat->kswapd) | |
1848 | return 0; | |
1849 | ||
1850 | pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); | |
1851 | if (IS_ERR(pgdat->kswapd)) { | |
1852 | /* failure at boot is fatal */ | |
1853 | BUG_ON(system_state == SYSTEM_BOOTING); | |
1854 | printk("Failed to start kswapd on node %d\n",nid); | |
1855 | ret = -1; | |
1856 | } | |
1857 | return ret; | |
1858 | } | |
1859 | ||
1da177e4 LT |
1860 | static int __init kswapd_init(void) |
1861 | { | |
3218ae14 | 1862 | int nid; |
69e05944 | 1863 | |
1da177e4 | 1864 | swap_setup(); |
9422ffba | 1865 | for_each_node_state(nid, N_HIGH_MEMORY) |
3218ae14 | 1866 | kswapd_run(nid); |
1da177e4 LT |
1867 | hotcpu_notifier(cpu_callback, 0); |
1868 | return 0; | |
1869 | } | |
1870 | ||
1871 | module_init(kswapd_init) | |
9eeff239 CL |
1872 | |
1873 | #ifdef CONFIG_NUMA | |
1874 | /* | |
1875 | * Zone reclaim mode | |
1876 | * | |
1877 | * If non-zero call zone_reclaim when the number of free pages falls below | |
1878 | * the watermarks. | |
9eeff239 CL |
1879 | */ |
1880 | int zone_reclaim_mode __read_mostly; | |
1881 | ||
1b2ffb78 CL |
1882 | #define RECLAIM_OFF 0 |
1883 | #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */ | |
1884 | #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ | |
1885 | #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ | |
1886 | ||
a92f7126 CL |
1887 | /* |
1888 | * Priority for ZONE_RECLAIM. This determines the fraction of pages | |
1889 | * of a node considered for each zone_reclaim. 4 scans 1/16th of | |
1890 | * a zone. | |
1891 | */ | |
1892 | #define ZONE_RECLAIM_PRIORITY 4 | |
1893 | ||
9614634f CL |
1894 | /* |
1895 | * Percentage of pages in a zone that must be unmapped for zone_reclaim to | |
1896 | * occur. | |
1897 | */ | |
1898 | int sysctl_min_unmapped_ratio = 1; | |
1899 | ||
0ff38490 CL |
1900 | /* |
1901 | * If the number of slab pages in a zone grows beyond this percentage then | |
1902 | * slab reclaim needs to occur. | |
1903 | */ | |
1904 | int sysctl_min_slab_ratio = 5; | |
1905 | ||
9eeff239 CL |
1906 | /* |
1907 | * Try to free up some pages from this zone through reclaim. | |
1908 | */ | |
179e9639 | 1909 | static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) |
9eeff239 | 1910 | { |
7fb2d46d | 1911 | /* Minimum pages needed in order to stay on node */ |
69e05944 | 1912 | const unsigned long nr_pages = 1 << order; |
9eeff239 CL |
1913 | struct task_struct *p = current; |
1914 | struct reclaim_state reclaim_state; | |
8695949a | 1915 | int priority; |
05ff5137 | 1916 | unsigned long nr_reclaimed = 0; |
179e9639 AM |
1917 | struct scan_control sc = { |
1918 | .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), | |
1919 | .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP), | |
69e05944 AM |
1920 | .swap_cluster_max = max_t(unsigned long, nr_pages, |
1921 | SWAP_CLUSTER_MAX), | |
179e9639 | 1922 | .gfp_mask = gfp_mask, |
d6277db4 | 1923 | .swappiness = vm_swappiness, |
66e1707b | 1924 | .isolate_pages = isolate_pages_global, |
179e9639 | 1925 | }; |
83e33a47 | 1926 | unsigned long slab_reclaimable; |
9eeff239 CL |
1927 | |
1928 | disable_swap_token(); | |
9eeff239 | 1929 | cond_resched(); |
d4f7796e CL |
1930 | /* |
1931 | * We need to be able to allocate from the reserves for RECLAIM_SWAP | |
1932 | * and we also need to be able to write out pages for RECLAIM_WRITE | |
1933 | * and RECLAIM_SWAP. | |
1934 | */ | |
1935 | p->flags |= PF_MEMALLOC | PF_SWAPWRITE; | |
9eeff239 CL |
1936 | reclaim_state.reclaimed_slab = 0; |
1937 | p->reclaim_state = &reclaim_state; | |
c84db23c | 1938 | |
0ff38490 CL |
1939 | if (zone_page_state(zone, NR_FILE_PAGES) - |
1940 | zone_page_state(zone, NR_FILE_MAPPED) > | |
1941 | zone->min_unmapped_pages) { | |
1942 | /* | |
1943 | * Free memory by calling shrink zone with increasing | |
1944 | * priorities until we have enough memory freed. | |
1945 | */ | |
1946 | priority = ZONE_RECLAIM_PRIORITY; | |
1947 | do { | |
3bb1a852 | 1948 | note_zone_scanning_priority(zone, priority); |
0ff38490 CL |
1949 | nr_reclaimed += shrink_zone(priority, zone, &sc); |
1950 | priority--; | |
1951 | } while (priority >= 0 && nr_reclaimed < nr_pages); | |
1952 | } | |
c84db23c | 1953 | |
83e33a47 CL |
1954 | slab_reclaimable = zone_page_state(zone, NR_SLAB_RECLAIMABLE); |
1955 | if (slab_reclaimable > zone->min_slab_pages) { | |
2a16e3f4 | 1956 | /* |
7fb2d46d | 1957 | * shrink_slab() does not currently allow us to determine how |
0ff38490 CL |
1958 | * many pages were freed in this zone. So we take the current |
1959 | * number of slab pages and shake the slab until it is reduced | |
1960 | * by the same nr_pages that we used for reclaiming unmapped | |
1961 | * pages. | |
2a16e3f4 | 1962 | * |
0ff38490 CL |
1963 | * Note that shrink_slab will free memory on all zones and may |
1964 | * take a long time. | |
2a16e3f4 | 1965 | */ |
0ff38490 | 1966 | while (shrink_slab(sc.nr_scanned, gfp_mask, order) && |
83e33a47 CL |
1967 | zone_page_state(zone, NR_SLAB_RECLAIMABLE) > |
1968 | slab_reclaimable - nr_pages) | |
0ff38490 | 1969 | ; |
83e33a47 CL |
1970 | |
1971 | /* | |
1972 | * Update nr_reclaimed by the number of slab pages we | |
1973 | * reclaimed from this zone. | |
1974 | */ | |
1975 | nr_reclaimed += slab_reclaimable - | |
1976 | zone_page_state(zone, NR_SLAB_RECLAIMABLE); | |
2a16e3f4 CL |
1977 | } |
1978 | ||
9eeff239 | 1979 | p->reclaim_state = NULL; |
d4f7796e | 1980 | current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); |
05ff5137 | 1981 | return nr_reclaimed >= nr_pages; |
9eeff239 | 1982 | } |
179e9639 AM |
1983 | |
1984 | int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) | |
1985 | { | |
179e9639 | 1986 | int node_id; |
d773ed6b | 1987 | int ret; |
179e9639 AM |
1988 | |
1989 | /* | |
0ff38490 CL |
1990 | * Zone reclaim reclaims unmapped file backed pages and |
1991 | * slab pages if we are over the defined limits. | |
34aa1330 | 1992 | * |
9614634f CL |
1993 | * A small portion of unmapped file backed pages is needed for |
1994 | * file I/O otherwise pages read by file I/O will be immediately | |
1995 | * thrown out if the zone is overallocated. So we do not reclaim | |
1996 | * if less than a specified percentage of the zone is used by | |
1997 | * unmapped file backed pages. | |
179e9639 | 1998 | */ |
34aa1330 | 1999 | if (zone_page_state(zone, NR_FILE_PAGES) - |
0ff38490 CL |
2000 | zone_page_state(zone, NR_FILE_MAPPED) <= zone->min_unmapped_pages |
2001 | && zone_page_state(zone, NR_SLAB_RECLAIMABLE) | |
2002 | <= zone->min_slab_pages) | |
9614634f | 2003 | return 0; |
179e9639 | 2004 | |
d773ed6b DR |
2005 | if (zone_is_all_unreclaimable(zone)) |
2006 | return 0; | |
2007 | ||
179e9639 | 2008 | /* |
d773ed6b | 2009 | * Do not scan if the allocation should not be delayed. |
179e9639 | 2010 | */ |
d773ed6b | 2011 | if (!(gfp_mask & __GFP_WAIT) || (current->flags & PF_MEMALLOC)) |
179e9639 AM |
2012 | return 0; |
2013 | ||
2014 | /* | |
2015 | * Only run zone reclaim on the local zone or on zones that do not | |
2016 | * have associated processors. This will favor the local processor | |
2017 | * over remote processors and spread off node memory allocations | |
2018 | * as wide as possible. | |
2019 | */ | |
89fa3024 | 2020 | node_id = zone_to_nid(zone); |
37c0708d | 2021 | if (node_state(node_id, N_CPU) && node_id != numa_node_id()) |
179e9639 | 2022 | return 0; |
d773ed6b DR |
2023 | |
2024 | if (zone_test_and_set_flag(zone, ZONE_RECLAIM_LOCKED)) | |
2025 | return 0; | |
2026 | ret = __zone_reclaim(zone, gfp_mask, order); | |
2027 | zone_clear_flag(zone, ZONE_RECLAIM_LOCKED); | |
2028 | ||
2029 | return ret; | |
179e9639 | 2030 | } |
9eeff239 | 2031 | #endif |