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> | |
22 | #include <linux/file.h> | |
23 | #include <linux/writeback.h> | |
24 | #include <linux/blkdev.h> | |
25 | #include <linux/buffer_head.h> /* for try_to_release_page(), | |
26 | buffer_heads_over_limit */ | |
27 | #include <linux/mm_inline.h> | |
28 | #include <linux/pagevec.h> | |
29 | #include <linux/backing-dev.h> | |
30 | #include <linux/rmap.h> | |
31 | #include <linux/topology.h> | |
32 | #include <linux/cpu.h> | |
33 | #include <linux/cpuset.h> | |
34 | #include <linux/notifier.h> | |
35 | #include <linux/rwsem.h> | |
248a0301 | 36 | #include <linux/delay.h> |
3218ae14 | 37 | #include <linux/kthread.h> |
1da177e4 LT |
38 | |
39 | #include <asm/tlbflush.h> | |
40 | #include <asm/div64.h> | |
41 | ||
42 | #include <linux/swapops.h> | |
43 | ||
0f8053a5 NP |
44 | #include "internal.h" |
45 | ||
1da177e4 | 46 | struct scan_control { |
1da177e4 LT |
47 | /* Incremented by the number of inactive pages that were scanned */ |
48 | unsigned long nr_scanned; | |
49 | ||
1da177e4 | 50 | /* This context's GFP mask */ |
6daa0e28 | 51 | gfp_t gfp_mask; |
1da177e4 LT |
52 | |
53 | int may_writepage; | |
54 | ||
f1fd1067 CL |
55 | /* Can pages be swapped as part of reclaim? */ |
56 | int may_swap; | |
57 | ||
1da177e4 LT |
58 | /* This context's SWAP_CLUSTER_MAX. If freeing memory for |
59 | * suspend, we effectively ignore SWAP_CLUSTER_MAX. | |
60 | * In this context, it doesn't matter that we scan the | |
61 | * whole list at once. */ | |
62 | int swap_cluster_max; | |
d6277db4 RW |
63 | |
64 | int swappiness; | |
1da177e4 LT |
65 | }; |
66 | ||
67 | /* | |
68 | * The list of shrinker callbacks used by to apply pressure to | |
69 | * ageable caches. | |
70 | */ | |
71 | struct shrinker { | |
72 | shrinker_t shrinker; | |
73 | struct list_head list; | |
74 | int seeks; /* seeks to recreate an obj */ | |
75 | long nr; /* objs pending delete */ | |
76 | }; | |
77 | ||
78 | #define lru_to_page(_head) (list_entry((_head)->prev, struct page, lru)) | |
79 | ||
80 | #ifdef ARCH_HAS_PREFETCH | |
81 | #define prefetch_prev_lru_page(_page, _base, _field) \ | |
82 | do { \ | |
83 | if ((_page)->lru.prev != _base) { \ | |
84 | struct page *prev; \ | |
85 | \ | |
86 | prev = lru_to_page(&(_page->lru)); \ | |
87 | prefetch(&prev->_field); \ | |
88 | } \ | |
89 | } while (0) | |
90 | #else | |
91 | #define prefetch_prev_lru_page(_page, _base, _field) do { } while (0) | |
92 | #endif | |
93 | ||
94 | #ifdef ARCH_HAS_PREFETCHW | |
95 | #define prefetchw_prev_lru_page(_page, _base, _field) \ | |
96 | do { \ | |
97 | if ((_page)->lru.prev != _base) { \ | |
98 | struct page *prev; \ | |
99 | \ | |
100 | prev = lru_to_page(&(_page->lru)); \ | |
101 | prefetchw(&prev->_field); \ | |
102 | } \ | |
103 | } while (0) | |
104 | #else | |
105 | #define prefetchw_prev_lru_page(_page, _base, _field) do { } while (0) | |
106 | #endif | |
107 | ||
108 | /* | |
109 | * From 0 .. 100. Higher means more swappy. | |
110 | */ | |
111 | int vm_swappiness = 60; | |
bd1e22b8 | 112 | long vm_total_pages; /* The total number of pages which the VM controls */ |
1da177e4 LT |
113 | |
114 | static LIST_HEAD(shrinker_list); | |
115 | static DECLARE_RWSEM(shrinker_rwsem); | |
116 | ||
117 | /* | |
118 | * Add a shrinker callback to be called from the vm | |
119 | */ | |
120 | struct shrinker *set_shrinker(int seeks, shrinker_t theshrinker) | |
121 | { | |
122 | struct shrinker *shrinker; | |
123 | ||
124 | shrinker = kmalloc(sizeof(*shrinker), GFP_KERNEL); | |
125 | if (shrinker) { | |
126 | shrinker->shrinker = theshrinker; | |
127 | shrinker->seeks = seeks; | |
128 | shrinker->nr = 0; | |
129 | down_write(&shrinker_rwsem); | |
130 | list_add_tail(&shrinker->list, &shrinker_list); | |
131 | up_write(&shrinker_rwsem); | |
132 | } | |
133 | return shrinker; | |
134 | } | |
135 | EXPORT_SYMBOL(set_shrinker); | |
136 | ||
137 | /* | |
138 | * Remove one | |
139 | */ | |
140 | void remove_shrinker(struct shrinker *shrinker) | |
141 | { | |
142 | down_write(&shrinker_rwsem); | |
143 | list_del(&shrinker->list); | |
144 | up_write(&shrinker_rwsem); | |
145 | kfree(shrinker); | |
146 | } | |
147 | EXPORT_SYMBOL(remove_shrinker); | |
148 | ||
149 | #define SHRINK_BATCH 128 | |
150 | /* | |
151 | * Call the shrink functions to age shrinkable caches | |
152 | * | |
153 | * Here we assume it costs one seek to replace a lru page and that it also | |
154 | * takes a seek to recreate a cache object. With this in mind we age equal | |
155 | * percentages of the lru and ageable caches. This should balance the seeks | |
156 | * generated by these structures. | |
157 | * | |
158 | * If the vm encounted mapped pages on the LRU it increase the pressure on | |
159 | * slab to avoid swapping. | |
160 | * | |
161 | * We do weird things to avoid (scanned*seeks*entries) overflowing 32 bits. | |
162 | * | |
163 | * `lru_pages' represents the number of on-LRU pages in all the zones which | |
164 | * are eligible for the caller's allocation attempt. It is used for balancing | |
165 | * slab reclaim versus page reclaim. | |
b15e0905 | 166 | * |
167 | * Returns the number of slab objects which we shrunk. | |
1da177e4 | 168 | */ |
69e05944 AM |
169 | unsigned long shrink_slab(unsigned long scanned, gfp_t gfp_mask, |
170 | unsigned long lru_pages) | |
1da177e4 LT |
171 | { |
172 | struct shrinker *shrinker; | |
69e05944 | 173 | unsigned long ret = 0; |
1da177e4 LT |
174 | |
175 | if (scanned == 0) | |
176 | scanned = SWAP_CLUSTER_MAX; | |
177 | ||
178 | if (!down_read_trylock(&shrinker_rwsem)) | |
b15e0905 | 179 | return 1; /* Assume we'll be able to shrink next time */ |
1da177e4 LT |
180 | |
181 | list_for_each_entry(shrinker, &shrinker_list, list) { | |
182 | unsigned long long delta; | |
183 | unsigned long total_scan; | |
ea164d73 | 184 | unsigned long max_pass = (*shrinker->shrinker)(0, gfp_mask); |
1da177e4 LT |
185 | |
186 | delta = (4 * scanned) / shrinker->seeks; | |
ea164d73 | 187 | delta *= max_pass; |
1da177e4 LT |
188 | do_div(delta, lru_pages + 1); |
189 | shrinker->nr += delta; | |
ea164d73 AA |
190 | if (shrinker->nr < 0) { |
191 | printk(KERN_ERR "%s: nr=%ld\n", | |
192 | __FUNCTION__, shrinker->nr); | |
193 | shrinker->nr = max_pass; | |
194 | } | |
195 | ||
196 | /* | |
197 | * Avoid risking looping forever due to too large nr value: | |
198 | * never try to free more than twice the estimate number of | |
199 | * freeable entries. | |
200 | */ | |
201 | if (shrinker->nr > max_pass * 2) | |
202 | shrinker->nr = max_pass * 2; | |
1da177e4 LT |
203 | |
204 | total_scan = shrinker->nr; | |
205 | shrinker->nr = 0; | |
206 | ||
207 | while (total_scan >= SHRINK_BATCH) { | |
208 | long this_scan = SHRINK_BATCH; | |
209 | int shrink_ret; | |
b15e0905 | 210 | int nr_before; |
1da177e4 | 211 | |
b15e0905 | 212 | nr_before = (*shrinker->shrinker)(0, gfp_mask); |
1da177e4 LT |
213 | shrink_ret = (*shrinker->shrinker)(this_scan, gfp_mask); |
214 | if (shrink_ret == -1) | |
215 | break; | |
b15e0905 | 216 | if (shrink_ret < nr_before) |
217 | ret += nr_before - shrink_ret; | |
1da177e4 LT |
218 | mod_page_state(slabs_scanned, this_scan); |
219 | total_scan -= this_scan; | |
220 | ||
221 | cond_resched(); | |
222 | } | |
223 | ||
224 | shrinker->nr += total_scan; | |
225 | } | |
226 | up_read(&shrinker_rwsem); | |
b15e0905 | 227 | return ret; |
1da177e4 LT |
228 | } |
229 | ||
230 | /* Called without lock on whether page is mapped, so answer is unstable */ | |
231 | static inline int page_mapping_inuse(struct page *page) | |
232 | { | |
233 | struct address_space *mapping; | |
234 | ||
235 | /* Page is in somebody's page tables. */ | |
236 | if (page_mapped(page)) | |
237 | return 1; | |
238 | ||
239 | /* Be more reluctant to reclaim swapcache than pagecache */ | |
240 | if (PageSwapCache(page)) | |
241 | return 1; | |
242 | ||
243 | mapping = page_mapping(page); | |
244 | if (!mapping) | |
245 | return 0; | |
246 | ||
247 | /* File is mmap'd by somebody? */ | |
248 | return mapping_mapped(mapping); | |
249 | } | |
250 | ||
251 | static inline int is_page_cache_freeable(struct page *page) | |
252 | { | |
253 | return page_count(page) - !!PagePrivate(page) == 2; | |
254 | } | |
255 | ||
256 | static int may_write_to_queue(struct backing_dev_info *bdi) | |
257 | { | |
930d9152 | 258 | if (current->flags & PF_SWAPWRITE) |
1da177e4 LT |
259 | return 1; |
260 | if (!bdi_write_congested(bdi)) | |
261 | return 1; | |
262 | if (bdi == current->backing_dev_info) | |
263 | return 1; | |
264 | return 0; | |
265 | } | |
266 | ||
267 | /* | |
268 | * We detected a synchronous write error writing a page out. Probably | |
269 | * -ENOSPC. We need to propagate that into the address_space for a subsequent | |
270 | * fsync(), msync() or close(). | |
271 | * | |
272 | * The tricky part is that after writepage we cannot touch the mapping: nothing | |
273 | * prevents it from being freed up. But we have a ref on the page and once | |
274 | * that page is locked, the mapping is pinned. | |
275 | * | |
276 | * We're allowed to run sleeping lock_page() here because we know the caller has | |
277 | * __GFP_FS. | |
278 | */ | |
279 | static void handle_write_error(struct address_space *mapping, | |
280 | struct page *page, int error) | |
281 | { | |
282 | lock_page(page); | |
283 | if (page_mapping(page) == mapping) { | |
284 | if (error == -ENOSPC) | |
285 | set_bit(AS_ENOSPC, &mapping->flags); | |
286 | else | |
287 | set_bit(AS_EIO, &mapping->flags); | |
288 | } | |
289 | unlock_page(page); | |
290 | } | |
291 | ||
04e62a29 CL |
292 | /* possible outcome of pageout() */ |
293 | typedef enum { | |
294 | /* failed to write page out, page is locked */ | |
295 | PAGE_KEEP, | |
296 | /* move page to the active list, page is locked */ | |
297 | PAGE_ACTIVATE, | |
298 | /* page has been sent to the disk successfully, page is unlocked */ | |
299 | PAGE_SUCCESS, | |
300 | /* page is clean and locked */ | |
301 | PAGE_CLEAN, | |
302 | } pageout_t; | |
303 | ||
1da177e4 | 304 | /* |
1742f19f AM |
305 | * pageout is called by shrink_page_list() for each dirty page. |
306 | * Calls ->writepage(). | |
1da177e4 | 307 | */ |
04e62a29 | 308 | static pageout_t pageout(struct page *page, struct address_space *mapping) |
1da177e4 LT |
309 | { |
310 | /* | |
311 | * If the page is dirty, only perform writeback if that write | |
312 | * will be non-blocking. To prevent this allocation from being | |
313 | * stalled by pagecache activity. But note that there may be | |
314 | * stalls if we need to run get_block(). We could test | |
315 | * PagePrivate for that. | |
316 | * | |
317 | * If this process is currently in generic_file_write() against | |
318 | * this page's queue, we can perform writeback even if that | |
319 | * will block. | |
320 | * | |
321 | * If the page is swapcache, write it back even if that would | |
322 | * block, for some throttling. This happens by accident, because | |
323 | * swap_backing_dev_info is bust: it doesn't reflect the | |
324 | * congestion state of the swapdevs. Easy to fix, if needed. | |
325 | * See swapfile.c:page_queue_congested(). | |
326 | */ | |
327 | if (!is_page_cache_freeable(page)) | |
328 | return PAGE_KEEP; | |
329 | if (!mapping) { | |
330 | /* | |
331 | * Some data journaling orphaned pages can have | |
332 | * page->mapping == NULL while being dirty with clean buffers. | |
333 | */ | |
323aca6c | 334 | if (PagePrivate(page)) { |
1da177e4 LT |
335 | if (try_to_free_buffers(page)) { |
336 | ClearPageDirty(page); | |
337 | printk("%s: orphaned page\n", __FUNCTION__); | |
338 | return PAGE_CLEAN; | |
339 | } | |
340 | } | |
341 | return PAGE_KEEP; | |
342 | } | |
343 | if (mapping->a_ops->writepage == NULL) | |
344 | return PAGE_ACTIVATE; | |
345 | if (!may_write_to_queue(mapping->backing_dev_info)) | |
346 | return PAGE_KEEP; | |
347 | ||
348 | if (clear_page_dirty_for_io(page)) { | |
349 | int res; | |
350 | struct writeback_control wbc = { | |
351 | .sync_mode = WB_SYNC_NONE, | |
352 | .nr_to_write = SWAP_CLUSTER_MAX, | |
111ebb6e OH |
353 | .range_start = 0, |
354 | .range_end = LLONG_MAX, | |
1da177e4 LT |
355 | .nonblocking = 1, |
356 | .for_reclaim = 1, | |
357 | }; | |
358 | ||
359 | SetPageReclaim(page); | |
360 | res = mapping->a_ops->writepage(page, &wbc); | |
361 | if (res < 0) | |
362 | handle_write_error(mapping, page, res); | |
994fc28c | 363 | if (res == AOP_WRITEPAGE_ACTIVATE) { |
1da177e4 LT |
364 | ClearPageReclaim(page); |
365 | return PAGE_ACTIVATE; | |
366 | } | |
367 | if (!PageWriteback(page)) { | |
368 | /* synchronous write or broken a_ops? */ | |
369 | ClearPageReclaim(page); | |
370 | } | |
371 | ||
372 | return PAGE_SUCCESS; | |
373 | } | |
374 | ||
375 | return PAGE_CLEAN; | |
376 | } | |
377 | ||
b20a3503 | 378 | int remove_mapping(struct address_space *mapping, struct page *page) |
49d2e9cc CL |
379 | { |
380 | if (!mapping) | |
381 | return 0; /* truncate got there first */ | |
382 | ||
383 | write_lock_irq(&mapping->tree_lock); | |
384 | ||
385 | /* | |
386 | * The non-racy check for busy page. It is critical to check | |
387 | * PageDirty _after_ making sure that the page is freeable and | |
388 | * not in use by anybody. (pagecache + us == 2) | |
389 | */ | |
390 | if (unlikely(page_count(page) != 2)) | |
391 | goto cannot_free; | |
392 | smp_rmb(); | |
393 | if (unlikely(PageDirty(page))) | |
394 | goto cannot_free; | |
395 | ||
396 | if (PageSwapCache(page)) { | |
397 | swp_entry_t swap = { .val = page_private(page) }; | |
398 | __delete_from_swap_cache(page); | |
399 | write_unlock_irq(&mapping->tree_lock); | |
400 | swap_free(swap); | |
401 | __put_page(page); /* The pagecache ref */ | |
402 | return 1; | |
403 | } | |
404 | ||
405 | __remove_from_page_cache(page); | |
406 | write_unlock_irq(&mapping->tree_lock); | |
407 | __put_page(page); | |
408 | return 1; | |
409 | ||
410 | cannot_free: | |
411 | write_unlock_irq(&mapping->tree_lock); | |
412 | return 0; | |
413 | } | |
414 | ||
1da177e4 | 415 | /* |
1742f19f | 416 | * shrink_page_list() returns the number of reclaimed pages |
1da177e4 | 417 | */ |
1742f19f AM |
418 | static unsigned long shrink_page_list(struct list_head *page_list, |
419 | struct scan_control *sc) | |
1da177e4 LT |
420 | { |
421 | LIST_HEAD(ret_pages); | |
422 | struct pagevec freed_pvec; | |
423 | int pgactivate = 0; | |
05ff5137 | 424 | unsigned long nr_reclaimed = 0; |
1da177e4 LT |
425 | |
426 | cond_resched(); | |
427 | ||
428 | pagevec_init(&freed_pvec, 1); | |
429 | while (!list_empty(page_list)) { | |
430 | struct address_space *mapping; | |
431 | struct page *page; | |
432 | int may_enter_fs; | |
433 | int referenced; | |
434 | ||
435 | cond_resched(); | |
436 | ||
437 | page = lru_to_page(page_list); | |
438 | list_del(&page->lru); | |
439 | ||
440 | if (TestSetPageLocked(page)) | |
441 | goto keep; | |
442 | ||
443 | BUG_ON(PageActive(page)); | |
444 | ||
445 | sc->nr_scanned++; | |
80e43426 CL |
446 | |
447 | if (!sc->may_swap && page_mapped(page)) | |
448 | goto keep_locked; | |
449 | ||
1da177e4 LT |
450 | /* Double the slab pressure for mapped and swapcache pages */ |
451 | if (page_mapped(page) || PageSwapCache(page)) | |
452 | sc->nr_scanned++; | |
453 | ||
454 | if (PageWriteback(page)) | |
455 | goto keep_locked; | |
456 | ||
f7b7fd8f | 457 | referenced = page_referenced(page, 1); |
1da177e4 LT |
458 | /* In active use or really unfreeable? Activate it. */ |
459 | if (referenced && page_mapping_inuse(page)) | |
460 | goto activate_locked; | |
461 | ||
462 | #ifdef CONFIG_SWAP | |
463 | /* | |
464 | * Anonymous process memory has backing store? | |
465 | * Try to allocate it some swap space here. | |
466 | */ | |
6e5ef1a9 | 467 | if (PageAnon(page) && !PageSwapCache(page)) |
1480a540 | 468 | if (!add_to_swap(page, GFP_ATOMIC)) |
1da177e4 | 469 | goto activate_locked; |
1da177e4 LT |
470 | #endif /* CONFIG_SWAP */ |
471 | ||
472 | mapping = page_mapping(page); | |
473 | may_enter_fs = (sc->gfp_mask & __GFP_FS) || | |
474 | (PageSwapCache(page) && (sc->gfp_mask & __GFP_IO)); | |
475 | ||
476 | /* | |
477 | * The page is mapped into the page tables of one or more | |
478 | * processes. Try to unmap it here. | |
479 | */ | |
480 | if (page_mapped(page) && mapping) { | |
a48d07af | 481 | switch (try_to_unmap(page, 0)) { |
1da177e4 LT |
482 | case SWAP_FAIL: |
483 | goto activate_locked; | |
484 | case SWAP_AGAIN: | |
485 | goto keep_locked; | |
486 | case SWAP_SUCCESS: | |
487 | ; /* try to free the page below */ | |
488 | } | |
489 | } | |
490 | ||
491 | if (PageDirty(page)) { | |
492 | if (referenced) | |
493 | goto keep_locked; | |
494 | if (!may_enter_fs) | |
495 | goto keep_locked; | |
52a8363e | 496 | if (!sc->may_writepage) |
1da177e4 LT |
497 | goto keep_locked; |
498 | ||
499 | /* Page is dirty, try to write it out here */ | |
500 | switch(pageout(page, mapping)) { | |
501 | case PAGE_KEEP: | |
502 | goto keep_locked; | |
503 | case PAGE_ACTIVATE: | |
504 | goto activate_locked; | |
505 | case PAGE_SUCCESS: | |
506 | if (PageWriteback(page) || PageDirty(page)) | |
507 | goto keep; | |
508 | /* | |
509 | * A synchronous write - probably a ramdisk. Go | |
510 | * ahead and try to reclaim the page. | |
511 | */ | |
512 | if (TestSetPageLocked(page)) | |
513 | goto keep; | |
514 | if (PageDirty(page) || PageWriteback(page)) | |
515 | goto keep_locked; | |
516 | mapping = page_mapping(page); | |
517 | case PAGE_CLEAN: | |
518 | ; /* try to free the page below */ | |
519 | } | |
520 | } | |
521 | ||
522 | /* | |
523 | * If the page has buffers, try to free the buffer mappings | |
524 | * associated with this page. If we succeed we try to free | |
525 | * the page as well. | |
526 | * | |
527 | * We do this even if the page is PageDirty(). | |
528 | * try_to_release_page() does not perform I/O, but it is | |
529 | * possible for a page to have PageDirty set, but it is actually | |
530 | * clean (all its buffers are clean). This happens if the | |
531 | * buffers were written out directly, with submit_bh(). ext3 | |
532 | * will do this, as well as the blockdev mapping. | |
533 | * try_to_release_page() will discover that cleanness and will | |
534 | * drop the buffers and mark the page clean - it can be freed. | |
535 | * | |
536 | * Rarely, pages can have buffers and no ->mapping. These are | |
537 | * the pages which were not successfully invalidated in | |
538 | * truncate_complete_page(). We try to drop those buffers here | |
539 | * and if that worked, and the page is no longer mapped into | |
540 | * process address space (page_count == 1) it can be freed. | |
541 | * Otherwise, leave the page on the LRU so it is swappable. | |
542 | */ | |
543 | if (PagePrivate(page)) { | |
544 | if (!try_to_release_page(page, sc->gfp_mask)) | |
545 | goto activate_locked; | |
546 | if (!mapping && page_count(page) == 1) | |
547 | goto free_it; | |
548 | } | |
549 | ||
49d2e9cc CL |
550 | if (!remove_mapping(mapping, page)) |
551 | goto keep_locked; | |
1da177e4 LT |
552 | |
553 | free_it: | |
554 | unlock_page(page); | |
05ff5137 | 555 | nr_reclaimed++; |
1da177e4 LT |
556 | if (!pagevec_add(&freed_pvec, page)) |
557 | __pagevec_release_nonlru(&freed_pvec); | |
558 | continue; | |
559 | ||
560 | activate_locked: | |
561 | SetPageActive(page); | |
562 | pgactivate++; | |
563 | keep_locked: | |
564 | unlock_page(page); | |
565 | keep: | |
566 | list_add(&page->lru, &ret_pages); | |
567 | BUG_ON(PageLRU(page)); | |
568 | } | |
569 | list_splice(&ret_pages, page_list); | |
570 | if (pagevec_count(&freed_pvec)) | |
571 | __pagevec_release_nonlru(&freed_pvec); | |
572 | mod_page_state(pgactivate, pgactivate); | |
05ff5137 | 573 | return nr_reclaimed; |
1da177e4 LT |
574 | } |
575 | ||
576 | /* | |
577 | * zone->lru_lock is heavily contended. Some of the functions that | |
578 | * shrink the lists perform better by taking out a batch of pages | |
579 | * and working on them outside the LRU lock. | |
580 | * | |
581 | * For pagecache intensive workloads, this function is the hottest | |
582 | * spot in the kernel (apart from copy_*_user functions). | |
583 | * | |
584 | * Appropriate locks must be held before calling this function. | |
585 | * | |
586 | * @nr_to_scan: The number of pages to look through on the list. | |
587 | * @src: The LRU list to pull pages off. | |
588 | * @dst: The temp list to put pages on to. | |
589 | * @scanned: The number of pages that were scanned. | |
590 | * | |
591 | * returns how many pages were moved onto *@dst. | |
592 | */ | |
69e05944 AM |
593 | static unsigned long isolate_lru_pages(unsigned long nr_to_scan, |
594 | struct list_head *src, struct list_head *dst, | |
595 | unsigned long *scanned) | |
1da177e4 | 596 | { |
69e05944 | 597 | unsigned long nr_taken = 0; |
1da177e4 | 598 | struct page *page; |
c9b02d97 | 599 | unsigned long scan; |
1da177e4 | 600 | |
c9b02d97 | 601 | for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) { |
7c8ee9a8 | 602 | struct list_head *target; |
1da177e4 LT |
603 | page = lru_to_page(src); |
604 | prefetchw_prev_lru_page(page, src, flags); | |
605 | ||
8d438f96 NP |
606 | BUG_ON(!PageLRU(page)); |
607 | ||
053837fc | 608 | list_del(&page->lru); |
7c8ee9a8 NP |
609 | target = src; |
610 | if (likely(get_page_unless_zero(page))) { | |
053837fc | 611 | /* |
7c8ee9a8 NP |
612 | * Be careful not to clear PageLRU until after we're |
613 | * sure the page is not being freed elsewhere -- the | |
614 | * page release code relies on it. | |
053837fc | 615 | */ |
7c8ee9a8 NP |
616 | ClearPageLRU(page); |
617 | target = dst; | |
618 | nr_taken++; | |
619 | } /* else it is being freed elsewhere */ | |
46453a6e | 620 | |
7c8ee9a8 | 621 | list_add(&page->lru, target); |
1da177e4 LT |
622 | } |
623 | ||
624 | *scanned = scan; | |
625 | return nr_taken; | |
626 | } | |
627 | ||
628 | /* | |
1742f19f AM |
629 | * shrink_inactive_list() is a helper for shrink_zone(). It returns the number |
630 | * of reclaimed pages | |
1da177e4 | 631 | */ |
1742f19f AM |
632 | static unsigned long shrink_inactive_list(unsigned long max_scan, |
633 | struct zone *zone, struct scan_control *sc) | |
1da177e4 LT |
634 | { |
635 | LIST_HEAD(page_list); | |
636 | struct pagevec pvec; | |
69e05944 | 637 | unsigned long nr_scanned = 0; |
05ff5137 | 638 | unsigned long nr_reclaimed = 0; |
1da177e4 LT |
639 | |
640 | pagevec_init(&pvec, 1); | |
641 | ||
642 | lru_add_drain(); | |
643 | spin_lock_irq(&zone->lru_lock); | |
69e05944 | 644 | do { |
1da177e4 | 645 | struct page *page; |
69e05944 AM |
646 | unsigned long nr_taken; |
647 | unsigned long nr_scan; | |
648 | unsigned long nr_freed; | |
1da177e4 LT |
649 | |
650 | nr_taken = isolate_lru_pages(sc->swap_cluster_max, | |
651 | &zone->inactive_list, | |
652 | &page_list, &nr_scan); | |
653 | zone->nr_inactive -= nr_taken; | |
654 | zone->pages_scanned += nr_scan; | |
655 | spin_unlock_irq(&zone->lru_lock); | |
656 | ||
69e05944 | 657 | nr_scanned += nr_scan; |
1742f19f | 658 | nr_freed = shrink_page_list(&page_list, sc); |
05ff5137 | 659 | nr_reclaimed += nr_freed; |
a74609fa NP |
660 | local_irq_disable(); |
661 | if (current_is_kswapd()) { | |
662 | __mod_page_state_zone(zone, pgscan_kswapd, nr_scan); | |
663 | __mod_page_state(kswapd_steal, nr_freed); | |
664 | } else | |
665 | __mod_page_state_zone(zone, pgscan_direct, nr_scan); | |
666 | __mod_page_state_zone(zone, pgsteal, nr_freed); | |
667 | ||
fb8d14e1 WF |
668 | if (nr_taken == 0) |
669 | goto done; | |
670 | ||
a74609fa | 671 | spin_lock(&zone->lru_lock); |
1da177e4 LT |
672 | /* |
673 | * Put back any unfreeable pages. | |
674 | */ | |
675 | while (!list_empty(&page_list)) { | |
676 | page = lru_to_page(&page_list); | |
8d438f96 NP |
677 | BUG_ON(PageLRU(page)); |
678 | SetPageLRU(page); | |
1da177e4 LT |
679 | list_del(&page->lru); |
680 | if (PageActive(page)) | |
681 | add_page_to_active_list(zone, page); | |
682 | else | |
683 | add_page_to_inactive_list(zone, page); | |
684 | if (!pagevec_add(&pvec, page)) { | |
685 | spin_unlock_irq(&zone->lru_lock); | |
686 | __pagevec_release(&pvec); | |
687 | spin_lock_irq(&zone->lru_lock); | |
688 | } | |
689 | } | |
69e05944 | 690 | } while (nr_scanned < max_scan); |
fb8d14e1 | 691 | spin_unlock(&zone->lru_lock); |
1da177e4 | 692 | done: |
fb8d14e1 | 693 | local_irq_enable(); |
1da177e4 | 694 | pagevec_release(&pvec); |
05ff5137 | 695 | return nr_reclaimed; |
1da177e4 LT |
696 | } |
697 | ||
698 | /* | |
699 | * This moves pages from the active list to the inactive list. | |
700 | * | |
701 | * We move them the other way if the page is referenced by one or more | |
702 | * processes, from rmap. | |
703 | * | |
704 | * If the pages are mostly unmapped, the processing is fast and it is | |
705 | * appropriate to hold zone->lru_lock across the whole operation. But if | |
706 | * the pages are mapped, the processing is slow (page_referenced()) so we | |
707 | * should drop zone->lru_lock around each page. It's impossible to balance | |
708 | * this, so instead we remove the pages from the LRU while processing them. | |
709 | * It is safe to rely on PG_active against the non-LRU pages in here because | |
710 | * nobody will play with that bit on a non-LRU page. | |
711 | * | |
712 | * The downside is that we have to touch page->_count against each page. | |
713 | * But we had to alter page->flags anyway. | |
714 | */ | |
1742f19f AM |
715 | static void shrink_active_list(unsigned long nr_pages, struct zone *zone, |
716 | struct scan_control *sc) | |
1da177e4 | 717 | { |
69e05944 | 718 | unsigned long pgmoved; |
1da177e4 | 719 | int pgdeactivate = 0; |
69e05944 | 720 | unsigned long pgscanned; |
1da177e4 LT |
721 | LIST_HEAD(l_hold); /* The pages which were snipped off */ |
722 | LIST_HEAD(l_inactive); /* Pages to go onto the inactive_list */ | |
723 | LIST_HEAD(l_active); /* Pages to go onto the active_list */ | |
724 | struct page *page; | |
725 | struct pagevec pvec; | |
726 | int reclaim_mapped = 0; | |
2903fb16 | 727 | |
6e5ef1a9 | 728 | if (sc->may_swap) { |
2903fb16 CL |
729 | long mapped_ratio; |
730 | long distress; | |
731 | long swap_tendency; | |
732 | ||
733 | /* | |
734 | * `distress' is a measure of how much trouble we're having | |
735 | * reclaiming pages. 0 -> no problems. 100 -> great trouble. | |
736 | */ | |
737 | distress = 100 >> zone->prev_priority; | |
738 | ||
739 | /* | |
740 | * The point of this algorithm is to decide when to start | |
741 | * reclaiming mapped memory instead of just pagecache. Work out | |
742 | * how much memory | |
743 | * is mapped. | |
744 | */ | |
f3dbd344 CL |
745 | mapped_ratio = ((global_page_state(NR_FILE_MAPPED) + |
746 | global_page_state(NR_ANON_PAGES)) * 100) / | |
bf02cf4b | 747 | vm_total_pages; |
2903fb16 CL |
748 | |
749 | /* | |
750 | * Now decide how much we really want to unmap some pages. The | |
751 | * mapped ratio is downgraded - just because there's a lot of | |
752 | * mapped memory doesn't necessarily mean that page reclaim | |
753 | * isn't succeeding. | |
754 | * | |
755 | * The distress ratio is important - we don't want to start | |
756 | * going oom. | |
757 | * | |
758 | * A 100% value of vm_swappiness overrides this algorithm | |
759 | * altogether. | |
760 | */ | |
d6277db4 | 761 | swap_tendency = mapped_ratio / 2 + distress + sc->swappiness; |
2903fb16 CL |
762 | |
763 | /* | |
764 | * Now use this metric to decide whether to start moving mapped | |
765 | * memory onto the inactive list. | |
766 | */ | |
767 | if (swap_tendency >= 100) | |
768 | reclaim_mapped = 1; | |
769 | } | |
1da177e4 LT |
770 | |
771 | lru_add_drain(); | |
772 | spin_lock_irq(&zone->lru_lock); | |
773 | pgmoved = isolate_lru_pages(nr_pages, &zone->active_list, | |
774 | &l_hold, &pgscanned); | |
775 | zone->pages_scanned += pgscanned; | |
776 | zone->nr_active -= pgmoved; | |
777 | spin_unlock_irq(&zone->lru_lock); | |
778 | ||
1da177e4 LT |
779 | while (!list_empty(&l_hold)) { |
780 | cond_resched(); | |
781 | page = lru_to_page(&l_hold); | |
782 | list_del(&page->lru); | |
783 | if (page_mapped(page)) { | |
784 | if (!reclaim_mapped || | |
785 | (total_swap_pages == 0 && PageAnon(page)) || | |
f7b7fd8f | 786 | page_referenced(page, 0)) { |
1da177e4 LT |
787 | list_add(&page->lru, &l_active); |
788 | continue; | |
789 | } | |
790 | } | |
791 | list_add(&page->lru, &l_inactive); | |
792 | } | |
793 | ||
794 | pagevec_init(&pvec, 1); | |
795 | pgmoved = 0; | |
796 | spin_lock_irq(&zone->lru_lock); | |
797 | while (!list_empty(&l_inactive)) { | |
798 | page = lru_to_page(&l_inactive); | |
799 | prefetchw_prev_lru_page(page, &l_inactive, flags); | |
8d438f96 NP |
800 | BUG_ON(PageLRU(page)); |
801 | SetPageLRU(page); | |
4c84cacf NP |
802 | BUG_ON(!PageActive(page)); |
803 | ClearPageActive(page); | |
804 | ||
1da177e4 LT |
805 | list_move(&page->lru, &zone->inactive_list); |
806 | pgmoved++; | |
807 | if (!pagevec_add(&pvec, page)) { | |
808 | zone->nr_inactive += pgmoved; | |
809 | spin_unlock_irq(&zone->lru_lock); | |
810 | pgdeactivate += pgmoved; | |
811 | pgmoved = 0; | |
812 | if (buffer_heads_over_limit) | |
813 | pagevec_strip(&pvec); | |
814 | __pagevec_release(&pvec); | |
815 | spin_lock_irq(&zone->lru_lock); | |
816 | } | |
817 | } | |
818 | zone->nr_inactive += pgmoved; | |
819 | pgdeactivate += pgmoved; | |
820 | if (buffer_heads_over_limit) { | |
821 | spin_unlock_irq(&zone->lru_lock); | |
822 | pagevec_strip(&pvec); | |
823 | spin_lock_irq(&zone->lru_lock); | |
824 | } | |
825 | ||
826 | pgmoved = 0; | |
827 | while (!list_empty(&l_active)) { | |
828 | page = lru_to_page(&l_active); | |
829 | prefetchw_prev_lru_page(page, &l_active, flags); | |
8d438f96 NP |
830 | BUG_ON(PageLRU(page)); |
831 | SetPageLRU(page); | |
1da177e4 LT |
832 | BUG_ON(!PageActive(page)); |
833 | list_move(&page->lru, &zone->active_list); | |
834 | pgmoved++; | |
835 | if (!pagevec_add(&pvec, page)) { | |
836 | zone->nr_active += pgmoved; | |
837 | pgmoved = 0; | |
838 | spin_unlock_irq(&zone->lru_lock); | |
839 | __pagevec_release(&pvec); | |
840 | spin_lock_irq(&zone->lru_lock); | |
841 | } | |
842 | } | |
843 | zone->nr_active += pgmoved; | |
a74609fa NP |
844 | spin_unlock(&zone->lru_lock); |
845 | ||
846 | __mod_page_state_zone(zone, pgrefill, pgscanned); | |
847 | __mod_page_state(pgdeactivate, pgdeactivate); | |
848 | local_irq_enable(); | |
1da177e4 | 849 | |
a74609fa | 850 | pagevec_release(&pvec); |
1da177e4 LT |
851 | } |
852 | ||
853 | /* | |
854 | * This is a basic per-zone page freer. Used by both kswapd and direct reclaim. | |
855 | */ | |
05ff5137 AM |
856 | static unsigned long shrink_zone(int priority, struct zone *zone, |
857 | struct scan_control *sc) | |
1da177e4 LT |
858 | { |
859 | unsigned long nr_active; | |
860 | unsigned long nr_inactive; | |
8695949a | 861 | unsigned long nr_to_scan; |
05ff5137 | 862 | unsigned long nr_reclaimed = 0; |
1da177e4 | 863 | |
53e9a615 MH |
864 | atomic_inc(&zone->reclaim_in_progress); |
865 | ||
1da177e4 LT |
866 | /* |
867 | * Add one to `nr_to_scan' just to make sure that the kernel will | |
868 | * slowly sift through the active list. | |
869 | */ | |
8695949a | 870 | zone->nr_scan_active += (zone->nr_active >> priority) + 1; |
1da177e4 LT |
871 | nr_active = zone->nr_scan_active; |
872 | if (nr_active >= sc->swap_cluster_max) | |
873 | zone->nr_scan_active = 0; | |
874 | else | |
875 | nr_active = 0; | |
876 | ||
8695949a | 877 | zone->nr_scan_inactive += (zone->nr_inactive >> priority) + 1; |
1da177e4 LT |
878 | nr_inactive = zone->nr_scan_inactive; |
879 | if (nr_inactive >= sc->swap_cluster_max) | |
880 | zone->nr_scan_inactive = 0; | |
881 | else | |
882 | nr_inactive = 0; | |
883 | ||
1da177e4 LT |
884 | while (nr_active || nr_inactive) { |
885 | if (nr_active) { | |
8695949a | 886 | nr_to_scan = min(nr_active, |
1da177e4 | 887 | (unsigned long)sc->swap_cluster_max); |
8695949a | 888 | nr_active -= nr_to_scan; |
1742f19f | 889 | shrink_active_list(nr_to_scan, zone, sc); |
1da177e4 LT |
890 | } |
891 | ||
892 | if (nr_inactive) { | |
8695949a | 893 | nr_to_scan = min(nr_inactive, |
1da177e4 | 894 | (unsigned long)sc->swap_cluster_max); |
8695949a | 895 | nr_inactive -= nr_to_scan; |
1742f19f AM |
896 | nr_reclaimed += shrink_inactive_list(nr_to_scan, zone, |
897 | sc); | |
1da177e4 LT |
898 | } |
899 | } | |
900 | ||
901 | throttle_vm_writeout(); | |
53e9a615 MH |
902 | |
903 | atomic_dec(&zone->reclaim_in_progress); | |
05ff5137 | 904 | return nr_reclaimed; |
1da177e4 LT |
905 | } |
906 | ||
907 | /* | |
908 | * This is the direct reclaim path, for page-allocating processes. We only | |
909 | * try to reclaim pages from zones which will satisfy the caller's allocation | |
910 | * request. | |
911 | * | |
912 | * We reclaim from a zone even if that zone is over pages_high. Because: | |
913 | * a) The caller may be trying to free *extra* pages to satisfy a higher-order | |
914 | * allocation or | |
915 | * b) The zones may be over pages_high but they must go *over* pages_high to | |
916 | * satisfy the `incremental min' zone defense algorithm. | |
917 | * | |
918 | * Returns the number of reclaimed pages. | |
919 | * | |
920 | * If a zone is deemed to be full of pinned pages then just give it a light | |
921 | * scan then give up on it. | |
922 | */ | |
1742f19f | 923 | static unsigned long shrink_zones(int priority, struct zone **zones, |
05ff5137 | 924 | struct scan_control *sc) |
1da177e4 | 925 | { |
05ff5137 | 926 | unsigned long nr_reclaimed = 0; |
1da177e4 LT |
927 | int i; |
928 | ||
929 | for (i = 0; zones[i] != NULL; i++) { | |
930 | struct zone *zone = zones[i]; | |
931 | ||
f3fe6512 | 932 | if (!populated_zone(zone)) |
1da177e4 LT |
933 | continue; |
934 | ||
9bf2229f | 935 | if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) |
1da177e4 LT |
936 | continue; |
937 | ||
8695949a CL |
938 | zone->temp_priority = priority; |
939 | if (zone->prev_priority > priority) | |
940 | zone->prev_priority = priority; | |
1da177e4 | 941 | |
8695949a | 942 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) |
1da177e4 LT |
943 | continue; /* Let kswapd poll it */ |
944 | ||
05ff5137 | 945 | nr_reclaimed += shrink_zone(priority, zone, sc); |
1da177e4 | 946 | } |
05ff5137 | 947 | return nr_reclaimed; |
1da177e4 LT |
948 | } |
949 | ||
950 | /* | |
951 | * This is the main entry point to direct page reclaim. | |
952 | * | |
953 | * If a full scan of the inactive list fails to free enough memory then we | |
954 | * are "out of memory" and something needs to be killed. | |
955 | * | |
956 | * If the caller is !__GFP_FS then the probability of a failure is reasonably | |
957 | * high - the zone may be full of dirty or under-writeback pages, which this | |
958 | * caller can't do much about. We kick pdflush and take explicit naps in the | |
959 | * hope that some of these pages can be written. But if the allocating task | |
960 | * holds filesystem locks which prevent writeout this might not work, and the | |
961 | * allocation attempt will fail. | |
962 | */ | |
69e05944 | 963 | unsigned long try_to_free_pages(struct zone **zones, gfp_t gfp_mask) |
1da177e4 LT |
964 | { |
965 | int priority; | |
966 | int ret = 0; | |
69e05944 | 967 | unsigned long total_scanned = 0; |
05ff5137 | 968 | unsigned long nr_reclaimed = 0; |
1da177e4 | 969 | struct reclaim_state *reclaim_state = current->reclaim_state; |
1da177e4 LT |
970 | unsigned long lru_pages = 0; |
971 | int i; | |
179e9639 AM |
972 | struct scan_control sc = { |
973 | .gfp_mask = gfp_mask, | |
974 | .may_writepage = !laptop_mode, | |
975 | .swap_cluster_max = SWAP_CLUSTER_MAX, | |
976 | .may_swap = 1, | |
d6277db4 | 977 | .swappiness = vm_swappiness, |
179e9639 | 978 | }; |
1da177e4 LT |
979 | |
980 | inc_page_state(allocstall); | |
981 | ||
982 | for (i = 0; zones[i] != NULL; i++) { | |
983 | struct zone *zone = zones[i]; | |
984 | ||
9bf2229f | 985 | if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) |
1da177e4 LT |
986 | continue; |
987 | ||
988 | zone->temp_priority = DEF_PRIORITY; | |
989 | lru_pages += zone->nr_active + zone->nr_inactive; | |
990 | } | |
991 | ||
992 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { | |
1da177e4 | 993 | sc.nr_scanned = 0; |
f7b7fd8f RR |
994 | if (!priority) |
995 | disable_swap_token(); | |
1742f19f | 996 | nr_reclaimed += shrink_zones(priority, zones, &sc); |
1da177e4 LT |
997 | shrink_slab(sc.nr_scanned, gfp_mask, lru_pages); |
998 | if (reclaim_state) { | |
05ff5137 | 999 | nr_reclaimed += reclaim_state->reclaimed_slab; |
1da177e4 LT |
1000 | reclaim_state->reclaimed_slab = 0; |
1001 | } | |
1002 | total_scanned += sc.nr_scanned; | |
05ff5137 | 1003 | if (nr_reclaimed >= sc.swap_cluster_max) { |
1da177e4 LT |
1004 | ret = 1; |
1005 | goto out; | |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * Try to write back as many pages as we just scanned. This | |
1010 | * tends to cause slow streaming writers to write data to the | |
1011 | * disk smoothly, at the dirtying rate, which is nice. But | |
1012 | * that's undesirable in laptop mode, where we *want* lumpy | |
1013 | * writeout. So in laptop mode, write out the whole world. | |
1014 | */ | |
179e9639 AM |
1015 | if (total_scanned > sc.swap_cluster_max + |
1016 | sc.swap_cluster_max / 2) { | |
687a21ce | 1017 | wakeup_pdflush(laptop_mode ? 0 : total_scanned); |
1da177e4 LT |
1018 | sc.may_writepage = 1; |
1019 | } | |
1020 | ||
1021 | /* Take a nap, wait for some writeback to complete */ | |
1022 | if (sc.nr_scanned && priority < DEF_PRIORITY - 2) | |
1023 | blk_congestion_wait(WRITE, HZ/10); | |
1024 | } | |
1025 | out: | |
1026 | for (i = 0; zones[i] != 0; i++) { | |
1027 | struct zone *zone = zones[i]; | |
1028 | ||
9bf2229f | 1029 | if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) |
1da177e4 LT |
1030 | continue; |
1031 | ||
1032 | zone->prev_priority = zone->temp_priority; | |
1033 | } | |
1034 | return ret; | |
1035 | } | |
1036 | ||
1037 | /* | |
1038 | * For kswapd, balance_pgdat() will work across all this node's zones until | |
1039 | * they are all at pages_high. | |
1040 | * | |
1da177e4 LT |
1041 | * Returns the number of pages which were actually freed. |
1042 | * | |
1043 | * There is special handling here for zones which are full of pinned pages. | |
1044 | * This can happen if the pages are all mlocked, or if they are all used by | |
1045 | * device drivers (say, ZONE_DMA). Or if they are all in use by hugetlb. | |
1046 | * What we do is to detect the case where all pages in the zone have been | |
1047 | * scanned twice and there has been zero successful reclaim. Mark the zone as | |
1048 | * dead and from now on, only perform a short scan. Basically we're polling | |
1049 | * the zone for when the problem goes away. | |
1050 | * | |
1051 | * kswapd scans the zones in the highmem->normal->dma direction. It skips | |
1052 | * zones which have free_pages > pages_high, but once a zone is found to have | |
1053 | * free_pages <= pages_high, we scan that zone and the lower zones regardless | |
1054 | * of the number of free pages in the lower zones. This interoperates with | |
1055 | * the page allocator fallback scheme to ensure that aging of pages is balanced | |
1056 | * across the zones. | |
1057 | */ | |
d6277db4 | 1058 | static unsigned long balance_pgdat(pg_data_t *pgdat, int order) |
1da177e4 | 1059 | { |
1da177e4 LT |
1060 | int all_zones_ok; |
1061 | int priority; | |
1062 | int i; | |
69e05944 | 1063 | unsigned long total_scanned; |
05ff5137 | 1064 | unsigned long nr_reclaimed; |
1da177e4 | 1065 | struct reclaim_state *reclaim_state = current->reclaim_state; |
179e9639 AM |
1066 | struct scan_control sc = { |
1067 | .gfp_mask = GFP_KERNEL, | |
1068 | .may_swap = 1, | |
d6277db4 RW |
1069 | .swap_cluster_max = SWAP_CLUSTER_MAX, |
1070 | .swappiness = vm_swappiness, | |
179e9639 | 1071 | }; |
1da177e4 LT |
1072 | |
1073 | loop_again: | |
1074 | total_scanned = 0; | |
05ff5137 | 1075 | nr_reclaimed = 0; |
c0bbbc73 | 1076 | sc.may_writepage = !laptop_mode; |
1da177e4 LT |
1077 | inc_page_state(pageoutrun); |
1078 | ||
1079 | for (i = 0; i < pgdat->nr_zones; i++) { | |
1080 | struct zone *zone = pgdat->node_zones + i; | |
1081 | ||
1082 | zone->temp_priority = DEF_PRIORITY; | |
1083 | } | |
1084 | ||
1085 | for (priority = DEF_PRIORITY; priority >= 0; priority--) { | |
1086 | int end_zone = 0; /* Inclusive. 0 = ZONE_DMA */ | |
1087 | unsigned long lru_pages = 0; | |
1088 | ||
f7b7fd8f RR |
1089 | /* The swap token gets in the way of swapout... */ |
1090 | if (!priority) | |
1091 | disable_swap_token(); | |
1092 | ||
1da177e4 LT |
1093 | all_zones_ok = 1; |
1094 | ||
d6277db4 RW |
1095 | /* |
1096 | * Scan in the highmem->dma direction for the highest | |
1097 | * zone which needs scanning | |
1098 | */ | |
1099 | for (i = pgdat->nr_zones - 1; i >= 0; i--) { | |
1100 | struct zone *zone = pgdat->node_zones + i; | |
1da177e4 | 1101 | |
d6277db4 RW |
1102 | if (!populated_zone(zone)) |
1103 | continue; | |
1da177e4 | 1104 | |
d6277db4 RW |
1105 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) |
1106 | continue; | |
1da177e4 | 1107 | |
d6277db4 RW |
1108 | if (!zone_watermark_ok(zone, order, zone->pages_high, |
1109 | 0, 0)) { | |
1110 | end_zone = i; | |
1111 | goto scan; | |
1da177e4 | 1112 | } |
1da177e4 | 1113 | } |
d6277db4 | 1114 | goto out; |
1da177e4 LT |
1115 | scan: |
1116 | for (i = 0; i <= end_zone; i++) { | |
1117 | struct zone *zone = pgdat->node_zones + i; | |
1118 | ||
1119 | lru_pages += zone->nr_active + zone->nr_inactive; | |
1120 | } | |
1121 | ||
1122 | /* | |
1123 | * Now scan the zone in the dma->highmem direction, stopping | |
1124 | * at the last zone which needs scanning. | |
1125 | * | |
1126 | * We do this because the page allocator works in the opposite | |
1127 | * direction. This prevents the page allocator from allocating | |
1128 | * pages behind kswapd's direction of progress, which would | |
1129 | * cause too much scanning of the lower zones. | |
1130 | */ | |
1131 | for (i = 0; i <= end_zone; i++) { | |
1132 | struct zone *zone = pgdat->node_zones + i; | |
b15e0905 | 1133 | int nr_slab; |
1da177e4 | 1134 | |
f3fe6512 | 1135 | if (!populated_zone(zone)) |
1da177e4 LT |
1136 | continue; |
1137 | ||
1138 | if (zone->all_unreclaimable && priority != DEF_PRIORITY) | |
1139 | continue; | |
1140 | ||
d6277db4 RW |
1141 | if (!zone_watermark_ok(zone, order, zone->pages_high, |
1142 | end_zone, 0)) | |
1143 | all_zones_ok = 0; | |
1da177e4 LT |
1144 | zone->temp_priority = priority; |
1145 | if (zone->prev_priority > priority) | |
1146 | zone->prev_priority = priority; | |
1147 | sc.nr_scanned = 0; | |
05ff5137 | 1148 | nr_reclaimed += shrink_zone(priority, zone, &sc); |
1da177e4 | 1149 | reclaim_state->reclaimed_slab = 0; |
b15e0905 | 1150 | nr_slab = shrink_slab(sc.nr_scanned, GFP_KERNEL, |
1151 | lru_pages); | |
05ff5137 | 1152 | nr_reclaimed += reclaim_state->reclaimed_slab; |
1da177e4 LT |
1153 | total_scanned += sc.nr_scanned; |
1154 | if (zone->all_unreclaimable) | |
1155 | continue; | |
b15e0905 | 1156 | if (nr_slab == 0 && zone->pages_scanned >= |
1157 | (zone->nr_active + zone->nr_inactive) * 4) | |
1da177e4 LT |
1158 | zone->all_unreclaimable = 1; |
1159 | /* | |
1160 | * If we've done a decent amount of scanning and | |
1161 | * the reclaim ratio is low, start doing writepage | |
1162 | * even in laptop mode | |
1163 | */ | |
1164 | if (total_scanned > SWAP_CLUSTER_MAX * 2 && | |
05ff5137 | 1165 | total_scanned > nr_reclaimed + nr_reclaimed / 2) |
1da177e4 LT |
1166 | sc.may_writepage = 1; |
1167 | } | |
1da177e4 LT |
1168 | if (all_zones_ok) |
1169 | break; /* kswapd: all done */ | |
1170 | /* | |
1171 | * OK, kswapd is getting into trouble. Take a nap, then take | |
1172 | * another pass across the zones. | |
1173 | */ | |
1174 | if (total_scanned && priority < DEF_PRIORITY - 2) | |
1175 | blk_congestion_wait(WRITE, HZ/10); | |
1176 | ||
1177 | /* | |
1178 | * We do this so kswapd doesn't build up large priorities for | |
1179 | * example when it is freeing in parallel with allocators. It | |
1180 | * matches the direct reclaim path behaviour in terms of impact | |
1181 | * on zone->*_priority. | |
1182 | */ | |
d6277db4 | 1183 | if (nr_reclaimed >= SWAP_CLUSTER_MAX) |
1da177e4 LT |
1184 | break; |
1185 | } | |
1186 | out: | |
1187 | for (i = 0; i < pgdat->nr_zones; i++) { | |
1188 | struct zone *zone = pgdat->node_zones + i; | |
1189 | ||
1190 | zone->prev_priority = zone->temp_priority; | |
1191 | } | |
1192 | if (!all_zones_ok) { | |
1193 | cond_resched(); | |
1194 | goto loop_again; | |
1195 | } | |
1196 | ||
05ff5137 | 1197 | return nr_reclaimed; |
1da177e4 LT |
1198 | } |
1199 | ||
1200 | /* | |
1201 | * The background pageout daemon, started as a kernel thread | |
1202 | * from the init process. | |
1203 | * | |
1204 | * This basically trickles out pages so that we have _some_ | |
1205 | * free memory available even if there is no other activity | |
1206 | * that frees anything up. This is needed for things like routing | |
1207 | * etc, where we otherwise might have all activity going on in | |
1208 | * asynchronous contexts that cannot page things out. | |
1209 | * | |
1210 | * If there are applications that are active memory-allocators | |
1211 | * (most normal use), this basically shouldn't matter. | |
1212 | */ | |
1213 | static int kswapd(void *p) | |
1214 | { | |
1215 | unsigned long order; | |
1216 | pg_data_t *pgdat = (pg_data_t*)p; | |
1217 | struct task_struct *tsk = current; | |
1218 | DEFINE_WAIT(wait); | |
1219 | struct reclaim_state reclaim_state = { | |
1220 | .reclaimed_slab = 0, | |
1221 | }; | |
1222 | cpumask_t cpumask; | |
1223 | ||
1da177e4 LT |
1224 | cpumask = node_to_cpumask(pgdat->node_id); |
1225 | if (!cpus_empty(cpumask)) | |
1226 | set_cpus_allowed(tsk, cpumask); | |
1227 | current->reclaim_state = &reclaim_state; | |
1228 | ||
1229 | /* | |
1230 | * Tell the memory management that we're a "memory allocator", | |
1231 | * and that if we need more memory we should get access to it | |
1232 | * regardless (see "__alloc_pages()"). "kswapd" should | |
1233 | * never get caught in the normal page freeing logic. | |
1234 | * | |
1235 | * (Kswapd normally doesn't need memory anyway, but sometimes | |
1236 | * you need a small amount of memory in order to be able to | |
1237 | * page out something else, and this flag essentially protects | |
1238 | * us from recursively trying to free more memory as we're | |
1239 | * trying to free the first piece of memory in the first place). | |
1240 | */ | |
930d9152 | 1241 | tsk->flags |= PF_MEMALLOC | PF_SWAPWRITE | PF_KSWAPD; |
1da177e4 LT |
1242 | |
1243 | order = 0; | |
1244 | for ( ; ; ) { | |
1245 | unsigned long new_order; | |
3e1d1d28 CL |
1246 | |
1247 | try_to_freeze(); | |
1da177e4 LT |
1248 | |
1249 | prepare_to_wait(&pgdat->kswapd_wait, &wait, TASK_INTERRUPTIBLE); | |
1250 | new_order = pgdat->kswapd_max_order; | |
1251 | pgdat->kswapd_max_order = 0; | |
1252 | if (order < new_order) { | |
1253 | /* | |
1254 | * Don't sleep if someone wants a larger 'order' | |
1255 | * allocation | |
1256 | */ | |
1257 | order = new_order; | |
1258 | } else { | |
1259 | schedule(); | |
1260 | order = pgdat->kswapd_max_order; | |
1261 | } | |
1262 | finish_wait(&pgdat->kswapd_wait, &wait); | |
1263 | ||
d6277db4 | 1264 | balance_pgdat(pgdat, order); |
1da177e4 LT |
1265 | } |
1266 | return 0; | |
1267 | } | |
1268 | ||
1269 | /* | |
1270 | * A zone is low on free memory, so wake its kswapd task to service it. | |
1271 | */ | |
1272 | void wakeup_kswapd(struct zone *zone, int order) | |
1273 | { | |
1274 | pg_data_t *pgdat; | |
1275 | ||
f3fe6512 | 1276 | if (!populated_zone(zone)) |
1da177e4 LT |
1277 | return; |
1278 | ||
1279 | pgdat = zone->zone_pgdat; | |
7fb1d9fc | 1280 | if (zone_watermark_ok(zone, order, zone->pages_low, 0, 0)) |
1da177e4 LT |
1281 | return; |
1282 | if (pgdat->kswapd_max_order < order) | |
1283 | pgdat->kswapd_max_order = order; | |
9bf2229f | 1284 | if (!cpuset_zone_allowed(zone, __GFP_HARDWALL)) |
1da177e4 | 1285 | return; |
8d0986e2 | 1286 | if (!waitqueue_active(&pgdat->kswapd_wait)) |
1da177e4 | 1287 | return; |
8d0986e2 | 1288 | wake_up_interruptible(&pgdat->kswapd_wait); |
1da177e4 LT |
1289 | } |
1290 | ||
1291 | #ifdef CONFIG_PM | |
1292 | /* | |
d6277db4 RW |
1293 | * Helper function for shrink_all_memory(). Tries to reclaim 'nr_pages' pages |
1294 | * from LRU lists system-wide, for given pass and priority, and returns the | |
1295 | * number of reclaimed pages | |
1296 | * | |
1297 | * For pass > 3 we also try to shrink the LRU lists that contain a few pages | |
1298 | */ | |
1299 | static unsigned long shrink_all_zones(unsigned long nr_pages, int pass, | |
1300 | int prio, struct scan_control *sc) | |
1301 | { | |
1302 | struct zone *zone; | |
1303 | unsigned long nr_to_scan, ret = 0; | |
1304 | ||
1305 | for_each_zone(zone) { | |
1306 | ||
1307 | if (!populated_zone(zone)) | |
1308 | continue; | |
1309 | ||
1310 | if (zone->all_unreclaimable && prio != DEF_PRIORITY) | |
1311 | continue; | |
1312 | ||
1313 | /* For pass = 0 we don't shrink the active list */ | |
1314 | if (pass > 0) { | |
1315 | zone->nr_scan_active += (zone->nr_active >> prio) + 1; | |
1316 | if (zone->nr_scan_active >= nr_pages || pass > 3) { | |
1317 | zone->nr_scan_active = 0; | |
1318 | nr_to_scan = min(nr_pages, zone->nr_active); | |
1319 | shrink_active_list(nr_to_scan, zone, sc); | |
1320 | } | |
1321 | } | |
1322 | ||
1323 | zone->nr_scan_inactive += (zone->nr_inactive >> prio) + 1; | |
1324 | if (zone->nr_scan_inactive >= nr_pages || pass > 3) { | |
1325 | zone->nr_scan_inactive = 0; | |
1326 | nr_to_scan = min(nr_pages, zone->nr_inactive); | |
1327 | ret += shrink_inactive_list(nr_to_scan, zone, sc); | |
1328 | if (ret >= nr_pages) | |
1329 | return ret; | |
1330 | } | |
1331 | } | |
1332 | ||
1333 | return ret; | |
1334 | } | |
1335 | ||
1336 | /* | |
1337 | * Try to free `nr_pages' of memory, system-wide, and return the number of | |
1338 | * freed pages. | |
1339 | * | |
1340 | * Rather than trying to age LRUs the aim is to preserve the overall | |
1341 | * LRU order by reclaiming preferentially | |
1342 | * inactive > active > active referenced > active mapped | |
1da177e4 | 1343 | */ |
69e05944 | 1344 | unsigned long shrink_all_memory(unsigned long nr_pages) |
1da177e4 | 1345 | { |
d6277db4 | 1346 | unsigned long lru_pages, nr_slab; |
69e05944 | 1347 | unsigned long ret = 0; |
d6277db4 RW |
1348 | int pass; |
1349 | struct reclaim_state reclaim_state; | |
1350 | struct zone *zone; | |
1351 | struct scan_control sc = { | |
1352 | .gfp_mask = GFP_KERNEL, | |
1353 | .may_swap = 0, | |
1354 | .swap_cluster_max = nr_pages, | |
1355 | .may_writepage = 1, | |
1356 | .swappiness = vm_swappiness, | |
1da177e4 LT |
1357 | }; |
1358 | ||
1359 | current->reclaim_state = &reclaim_state; | |
69e05944 | 1360 | |
d6277db4 RW |
1361 | lru_pages = 0; |
1362 | for_each_zone(zone) | |
1363 | lru_pages += zone->nr_active + zone->nr_inactive; | |
1364 | ||
9a865ffa | 1365 | nr_slab = global_page_state(NR_SLAB); |
d6277db4 RW |
1366 | /* If slab caches are huge, it's better to hit them first */ |
1367 | while (nr_slab >= lru_pages) { | |
1368 | reclaim_state.reclaimed_slab = 0; | |
1369 | shrink_slab(nr_pages, sc.gfp_mask, lru_pages); | |
1370 | if (!reclaim_state.reclaimed_slab) | |
1da177e4 | 1371 | break; |
d6277db4 RW |
1372 | |
1373 | ret += reclaim_state.reclaimed_slab; | |
1374 | if (ret >= nr_pages) | |
1375 | goto out; | |
1376 | ||
1377 | nr_slab -= reclaim_state.reclaimed_slab; | |
1da177e4 | 1378 | } |
d6277db4 RW |
1379 | |
1380 | /* | |
1381 | * We try to shrink LRUs in 5 passes: | |
1382 | * 0 = Reclaim from inactive_list only | |
1383 | * 1 = Reclaim from active list but don't reclaim mapped | |
1384 | * 2 = 2nd pass of type 1 | |
1385 | * 3 = Reclaim mapped (normal reclaim) | |
1386 | * 4 = 2nd pass of type 3 | |
1387 | */ | |
1388 | for (pass = 0; pass < 5; pass++) { | |
1389 | int prio; | |
1390 | ||
1391 | /* Needed for shrinking slab caches later on */ | |
1392 | if (!lru_pages) | |
1393 | for_each_zone(zone) { | |
1394 | lru_pages += zone->nr_active; | |
1395 | lru_pages += zone->nr_inactive; | |
1396 | } | |
1397 | ||
1398 | /* Force reclaiming mapped pages in the passes #3 and #4 */ | |
1399 | if (pass > 2) { | |
1400 | sc.may_swap = 1; | |
1401 | sc.swappiness = 100; | |
1402 | } | |
1403 | ||
1404 | for (prio = DEF_PRIORITY; prio >= 0; prio--) { | |
1405 | unsigned long nr_to_scan = nr_pages - ret; | |
1406 | ||
d6277db4 | 1407 | sc.nr_scanned = 0; |
d6277db4 RW |
1408 | ret += shrink_all_zones(nr_to_scan, prio, pass, &sc); |
1409 | if (ret >= nr_pages) | |
1410 | goto out; | |
1411 | ||
1412 | reclaim_state.reclaimed_slab = 0; | |
1413 | shrink_slab(sc.nr_scanned, sc.gfp_mask, lru_pages); | |
1414 | ret += reclaim_state.reclaimed_slab; | |
1415 | if (ret >= nr_pages) | |
1416 | goto out; | |
1417 | ||
1418 | if (sc.nr_scanned && prio < DEF_PRIORITY - 2) | |
1419 | blk_congestion_wait(WRITE, HZ / 10); | |
1420 | } | |
1421 | ||
1422 | lru_pages = 0; | |
248a0301 | 1423 | } |
d6277db4 RW |
1424 | |
1425 | /* | |
1426 | * If ret = 0, we could not shrink LRUs, but there may be something | |
1427 | * in slab caches | |
1428 | */ | |
1429 | if (!ret) | |
1430 | do { | |
1431 | reclaim_state.reclaimed_slab = 0; | |
1432 | shrink_slab(nr_pages, sc.gfp_mask, lru_pages); | |
1433 | ret += reclaim_state.reclaimed_slab; | |
1434 | } while (ret < nr_pages && reclaim_state.reclaimed_slab > 0); | |
1435 | ||
1436 | out: | |
1da177e4 | 1437 | current->reclaim_state = NULL; |
d6277db4 | 1438 | |
1da177e4 LT |
1439 | return ret; |
1440 | } | |
1441 | #endif | |
1442 | ||
1443 | #ifdef CONFIG_HOTPLUG_CPU | |
1444 | /* It's optimal to keep kswapds on the same CPUs as their memory, but | |
1445 | not required for correctness. So if the last cpu in a node goes | |
1446 | away, we get changed to run anywhere: as the first one comes back, | |
1447 | restore their cpu bindings. */ | |
9c7b216d | 1448 | static int __devinit cpu_callback(struct notifier_block *nfb, |
69e05944 | 1449 | unsigned long action, void *hcpu) |
1da177e4 LT |
1450 | { |
1451 | pg_data_t *pgdat; | |
1452 | cpumask_t mask; | |
1453 | ||
1454 | if (action == CPU_ONLINE) { | |
ec936fc5 | 1455 | for_each_online_pgdat(pgdat) { |
1da177e4 LT |
1456 | mask = node_to_cpumask(pgdat->node_id); |
1457 | if (any_online_cpu(mask) != NR_CPUS) | |
1458 | /* One of our CPUs online: restore mask */ | |
1459 | set_cpus_allowed(pgdat->kswapd, mask); | |
1460 | } | |
1461 | } | |
1462 | return NOTIFY_OK; | |
1463 | } | |
1464 | #endif /* CONFIG_HOTPLUG_CPU */ | |
1465 | ||
3218ae14 YG |
1466 | /* |
1467 | * This kswapd start function will be called by init and node-hot-add. | |
1468 | * On node-hot-add, kswapd will moved to proper cpus if cpus are hot-added. | |
1469 | */ | |
1470 | int kswapd_run(int nid) | |
1471 | { | |
1472 | pg_data_t *pgdat = NODE_DATA(nid); | |
1473 | int ret = 0; | |
1474 | ||
1475 | if (pgdat->kswapd) | |
1476 | return 0; | |
1477 | ||
1478 | pgdat->kswapd = kthread_run(kswapd, pgdat, "kswapd%d", nid); | |
1479 | if (IS_ERR(pgdat->kswapd)) { | |
1480 | /* failure at boot is fatal */ | |
1481 | BUG_ON(system_state == SYSTEM_BOOTING); | |
1482 | printk("Failed to start kswapd on node %d\n",nid); | |
1483 | ret = -1; | |
1484 | } | |
1485 | return ret; | |
1486 | } | |
1487 | ||
1da177e4 LT |
1488 | static int __init kswapd_init(void) |
1489 | { | |
3218ae14 | 1490 | int nid; |
69e05944 | 1491 | |
1da177e4 | 1492 | swap_setup(); |
3218ae14 YG |
1493 | for_each_online_node(nid) |
1494 | kswapd_run(nid); | |
1da177e4 LT |
1495 | hotcpu_notifier(cpu_callback, 0); |
1496 | return 0; | |
1497 | } | |
1498 | ||
1499 | module_init(kswapd_init) | |
9eeff239 CL |
1500 | |
1501 | #ifdef CONFIG_NUMA | |
1502 | /* | |
1503 | * Zone reclaim mode | |
1504 | * | |
1505 | * If non-zero call zone_reclaim when the number of free pages falls below | |
1506 | * the watermarks. | |
1507 | * | |
1508 | * In the future we may add flags to the mode. However, the page allocator | |
1509 | * should only have to check that zone_reclaim_mode != 0 before calling | |
1510 | * zone_reclaim(). | |
1511 | */ | |
1512 | int zone_reclaim_mode __read_mostly; | |
1513 | ||
1b2ffb78 CL |
1514 | #define RECLAIM_OFF 0 |
1515 | #define RECLAIM_ZONE (1<<0) /* Run shrink_cache on the zone */ | |
1516 | #define RECLAIM_WRITE (1<<1) /* Writeout pages during reclaim */ | |
1517 | #define RECLAIM_SWAP (1<<2) /* Swap pages out during reclaim */ | |
2a16e3f4 | 1518 | #define RECLAIM_SLAB (1<<3) /* Do a global slab shrink if the zone is out of memory */ |
1b2ffb78 | 1519 | |
a92f7126 CL |
1520 | /* |
1521 | * Priority for ZONE_RECLAIM. This determines the fraction of pages | |
1522 | * of a node considered for each zone_reclaim. 4 scans 1/16th of | |
1523 | * a zone. | |
1524 | */ | |
1525 | #define ZONE_RECLAIM_PRIORITY 4 | |
1526 | ||
9eeff239 CL |
1527 | /* |
1528 | * Try to free up some pages from this zone through reclaim. | |
1529 | */ | |
179e9639 | 1530 | static int __zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) |
9eeff239 | 1531 | { |
7fb2d46d | 1532 | /* Minimum pages needed in order to stay on node */ |
69e05944 | 1533 | const unsigned long nr_pages = 1 << order; |
9eeff239 CL |
1534 | struct task_struct *p = current; |
1535 | struct reclaim_state reclaim_state; | |
8695949a | 1536 | int priority; |
05ff5137 | 1537 | unsigned long nr_reclaimed = 0; |
179e9639 AM |
1538 | struct scan_control sc = { |
1539 | .may_writepage = !!(zone_reclaim_mode & RECLAIM_WRITE), | |
1540 | .may_swap = !!(zone_reclaim_mode & RECLAIM_SWAP), | |
69e05944 AM |
1541 | .swap_cluster_max = max_t(unsigned long, nr_pages, |
1542 | SWAP_CLUSTER_MAX), | |
179e9639 | 1543 | .gfp_mask = gfp_mask, |
d6277db4 | 1544 | .swappiness = vm_swappiness, |
179e9639 | 1545 | }; |
9eeff239 CL |
1546 | |
1547 | disable_swap_token(); | |
9eeff239 | 1548 | cond_resched(); |
d4f7796e CL |
1549 | /* |
1550 | * We need to be able to allocate from the reserves for RECLAIM_SWAP | |
1551 | * and we also need to be able to write out pages for RECLAIM_WRITE | |
1552 | * and RECLAIM_SWAP. | |
1553 | */ | |
1554 | p->flags |= PF_MEMALLOC | PF_SWAPWRITE; | |
9eeff239 CL |
1555 | reclaim_state.reclaimed_slab = 0; |
1556 | p->reclaim_state = &reclaim_state; | |
c84db23c | 1557 | |
a92f7126 CL |
1558 | /* |
1559 | * Free memory by calling shrink zone with increasing priorities | |
1560 | * until we have enough memory freed. | |
1561 | */ | |
8695949a | 1562 | priority = ZONE_RECLAIM_PRIORITY; |
a92f7126 | 1563 | do { |
05ff5137 | 1564 | nr_reclaimed += shrink_zone(priority, zone, &sc); |
8695949a | 1565 | priority--; |
05ff5137 | 1566 | } while (priority >= 0 && nr_reclaimed < nr_pages); |
c84db23c | 1567 | |
05ff5137 | 1568 | if (nr_reclaimed < nr_pages && (zone_reclaim_mode & RECLAIM_SLAB)) { |
2a16e3f4 | 1569 | /* |
7fb2d46d CL |
1570 | * shrink_slab() does not currently allow us to determine how |
1571 | * many pages were freed in this zone. So we just shake the slab | |
1572 | * a bit and then go off node for this particular allocation | |
1573 | * despite possibly having freed enough memory to allocate in | |
1574 | * this zone. If we freed local memory then the next | |
1575 | * allocations will be local again. | |
2a16e3f4 CL |
1576 | * |
1577 | * shrink_slab will free memory on all zones and may take | |
1578 | * a long time. | |
1579 | */ | |
1580 | shrink_slab(sc.nr_scanned, gfp_mask, order); | |
2a16e3f4 CL |
1581 | } |
1582 | ||
9eeff239 | 1583 | p->reclaim_state = NULL; |
d4f7796e | 1584 | current->flags &= ~(PF_MEMALLOC | PF_SWAPWRITE); |
05ff5137 | 1585 | return nr_reclaimed >= nr_pages; |
9eeff239 | 1586 | } |
179e9639 AM |
1587 | |
1588 | int zone_reclaim(struct zone *zone, gfp_t gfp_mask, unsigned int order) | |
1589 | { | |
1590 | cpumask_t mask; | |
1591 | int node_id; | |
1592 | ||
1593 | /* | |
34aa1330 CL |
1594 | * Do not reclaim if there are not enough reclaimable pages in this |
1595 | * zone that would satify this allocations. | |
1596 | * | |
1597 | * All unmapped pagecache pages are reclaimable. | |
1598 | * | |
1599 | * Both counters may be temporarily off a bit so we use | |
1600 | * SWAP_CLUSTER_MAX as the boundary. It may also be good to | |
1601 | * leave a few frequently used unmapped pagecache pages around. | |
179e9639 | 1602 | */ |
34aa1330 CL |
1603 | if (zone_page_state(zone, NR_FILE_PAGES) - |
1604 | zone_page_state(zone, NR_FILE_MAPPED) < SWAP_CLUSTER_MAX) | |
179e9639 AM |
1605 | return 0; |
1606 | ||
1607 | /* | |
1608 | * Avoid concurrent zone reclaims, do not reclaim in a zone that does | |
1609 | * not have reclaimable pages and if we should not delay the allocation | |
1610 | * then do not scan. | |
1611 | */ | |
1612 | if (!(gfp_mask & __GFP_WAIT) || | |
1613 | zone->all_unreclaimable || | |
1614 | atomic_read(&zone->reclaim_in_progress) > 0 || | |
1615 | (current->flags & PF_MEMALLOC)) | |
1616 | return 0; | |
1617 | ||
1618 | /* | |
1619 | * Only run zone reclaim on the local zone or on zones that do not | |
1620 | * have associated processors. This will favor the local processor | |
1621 | * over remote processors and spread off node memory allocations | |
1622 | * as wide as possible. | |
1623 | */ | |
1624 | node_id = zone->zone_pgdat->node_id; | |
1625 | mask = node_to_cpumask(node_id); | |
1626 | if (!cpus_empty(mask) && node_id != numa_node_id()) | |
1627 | return 0; | |
1628 | return __zone_reclaim(zone, gfp_mask, order); | |
1629 | } | |
9eeff239 | 1630 | #endif |