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